dotnet / maui

.NET MAUI is the .NET Multi-platform App UI, a framework for building native device applications spanning mobile, tablet, and desktop.
https://dot.net/maui
MIT License
21.83k stars 1.67k forks source link

App crashes when calling ItemsView.ScrollTo on unloaded CollectionView #23014

Open spadapet opened 3 weeks ago

spadapet commented 3 weeks ago

Description

Exceptions are thrown on all platforms when calling ScrollTo on an unloaded CollectionView (or probably any ItemsView). While it sounds odd to try and scroll an unloaded ItemsView, it can easily happen by accident if ScrollTo is called in an event handler. For example, here is a user project that triggers the bug in a SelectionChanged handler:

https://github.com/YBTopaz8/Dimmer-MAUI/blob/f5366d12d8a59a0d70ce80b201891a672d0cd13d/Dimmer/Views/Desktop/HomeD.xaml.cs#L32

SelectionChanged is triggered when removing CollectionView from its parent because the BindingContext changes. When MAUI Full Page Hot Reload in Visual Studio 17.10 reloads the XAML, it can also trigger this issue.

Steps to Reproduce

REPRO:

  1. Clone test repo:
  2. Open the test project solution file in Visual Studio 2022 17.10+
  3. F5 to build and debug the app on Windows
  4. Click the button: image

RESULT:

App crashes with a NullReferenceException. On Windows, the ItemsViewHandler.Windows.cs file is still listening to scroll requests after CleanUpCollectionViewSource() is called.

On iOS or Android emulator, different exceptions are thrown.

Android: image

iOS: An index out of range exception

Link to public reproduction project repository

https://github.com/spadapet/maui_bugs.git

Version with bug

8.0.3 GA

Is this a regression from previous behavior?

Not sure, did not test other versions

Last version that worked well

Unknown/Other

Affected platforms

iOS, Android, Windows, macOS

Affected platform versions

No response

Did you find any workaround?

Skip calling ItemsView.ScrollTo if the IsLoaded property is false.

Relevant log output

No response

github-actions[bot] commented 3 weeks ago

Hi I'm an AI powered bot that finds similar issues based off the issue title.

Please view the issues below to see if they solve your problem, and if the issue describes your problem please consider closing this one and thumbs upping the other issue to help us prioritize it. Thank you!

Open similar issues:

Closed similar issues:

Note: You can give me feedback by thumbs upping or thumbs downing this comment.

Zhanglirong-Winnie commented 3 weeks ago

Verified this issue with Visual Studio 17.11.0 Preview 2.0 (8.0.40 & 8.0.21 & 8.0.3). Can repro this issue with sample project.