WinUI app crashing when you scroll? Here's how to fix it

Years ago, Microsoft introduced this bug in WinUI 2, which can cause WinUI 2 apps to crash then you scroll to the bottom of a scrollable area in a ScrollViewer. You may have noticed this bug in built-in Microsoft apps such as the Microsoft Store, as they use WinUI. This bug was partially addressed in Windows 11, but can still occur. 

Luckily, the bug only occurs with WinUI 2 and not plain UWP, so, to fix it, we can just use the UWP ScrollViewer style instead. To do this, just apply a blank style to all or just the offending ScrollViewers like so:

<Style TargetType="ScrollViewer"/>

 When you create your own style, it inherits from the UWP styles instead of the WinUI 2 styles, as explained here.

You could apply it to an individual ScrollViewer like this:

<ScrollViewer.Style> <Style TargetType="ScrollViewer"/>

</ScrollViewer.Style>

Or all of them in App.xaml like so:

<Application.Resources>

<controls:XamlControlsResources>

<Style TargetType="ScrollViewer"/>

</controls:XamlControlsResources>

</Application.Resources>

Could even give it a key like so and then reference it wherever needed:

<Application.Resources>

<controls:XamlControlsResources>

<Style x:Key="UWPScrollViewerStyle" TargetType="ScrollViewer"/>

</controls:XamlControlsResources>

</Application.Resources>

<ScrollViewer Style="{StaticResource UWPScrollViewerStyle}"/>

The Style class can also be initialised and applied from code if need be.

C#:

ScrollViewer.Style = new Style(typeof(ScrollViewer));

VB:

ScrollViewer.Style = New Style(GetType(ScrollViewer))


This snippet is available in Codly. Click the download link below to download the snippet. If you don't have Codly, it is available here in the Microsoft Store.

Comments

Popular posts from this blog

How to show placeholder text in a WPF TextBox in C# and VB

How to enable dark title bar in WPF and Windows Forms/WinForms apps in C# and VB

How to use modern icons in XAML in WPF on Windows 10 and 11

How to change the colour of a WPF or Windows Forms/WinForms title bar in Windows 11 in C# and VB

Microsoft WebView2: How to check if the WebView2 runtime is installed in C# and VB