Xamarin.Forms
Xamarin.Forms copied to clipboard
[Bug] [iOS] Setting CollectionView.ItemsSource to null after selecting an item crashes
Description
Setting the ItemsSource property of a CollectionView to null while there is a selected item causes an exception to be thrown. This also happens if ItemsSource is set to null indirectly, in my case I found out while trying to set the Page.BindingContext to null.
Steps to Reproduce
- Have a collection view.
- Select an item.
- Set ItemsSource to null.
Expected Behavior
The collection view is empty without an error.
Actual Behavior
System.IndexOutOfRangeException IItemsViewSource is empty
Basic Information
- Version with issue: 5.0.0.2291
Reproduction Link
https://github.com/GalaxiaGuy/xf-bugs/tree/bug/collectionview-null-binding
The crash ultimately happens when the UICollectionViewController calls GetIndexForItem on the IItemsViewSource.
ListSource, for example searches for the item and returns NSIndexPath.Create(-1, -1) if it is not found (which is what happens when you change the ItemsSource to something not null and the selected item is not in the new source).
EmptySource however throws IndexOutOfRangeException in this situation (actually, it throws unconditionally).
It seems the easiest solution is to make EmptySource return NSIndexPath.Create(-1, -1) instead. This would match the behavior of most IndexOf methods, but I don't know if there are wider considerations.
I am having this same issue - any workarounds identified?
I was having the same problem on ios. Setting the selected item to null before setting the ItemSource to null solved the problem for me.
I am experiencing the same issue. The only difference is that I am using a library cold Prism. Whenever you dismiss the screen, the Prism library "Destroys" the page by setting its BindingContext to null. Unfortunately, when this happens and there's a SelectedItem on the CollectionView the above-mentioned exception is raised. Thankfully, for me the workaround was setting the SelectedItem to null before that happens and luckily Prism has IDestructible.Destroy interface where you can do "cleanup", in this case it was setting the SelectedItem to null
I am experiencing the same issue. The only difference is that I am using a library cold Prism. Whenever you dismiss the screen, the Prism library "Destroys" the page by setting its
BindingContexttonull. Unfortunately, when this happens and there's aSelectedItemon theCollectionViewthe above-mentioned exception is raised. Thankfully, for me the workaround was setting theSelectedItemtonullbefore that happens and luckily Prism hasIDestructible.Destroyinterface where you can do "cleanup", in this case it was setting theSelectedItemtonull
Not using Prism, but FreshMVVM. Same issue when the page is dismissed. I wish there was a solution that didn't involve nulling this out every time.