Skip to content

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

@spadapet

Description

@spadapet

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

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions