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))
Comments
Post a Comment