xamarin / Xamarin.Forms

Xamarin.Forms is no longer supported. Migrate your apps to .NET MAUI.
https://aka.ms/xamarin-upgrade
Other
5.63k stars 1.88k forks source link

[Bug] CarouselView remove and change if scrolled to end causes System.ArgumentOutOfRangeException on Android #13905

Open nziosys opened 3 years ago

nziosys commented 3 years ago

Description

I know, this is very specific case, but it disables me implementing my specific app logic. The app crashes (System.ArgumentOutOfRangeException) if you scroll to the end of the carouselView and than change an element of the bound observable collection and remove another. All these conditions need to be fullfiled to get that Bug and exception. The scenario is - the user scrolls through the carousel and new data is comming from the server, so I need to remove, change and add some items. Concider that is works on iOS, it also works if you only remove or only change items, and even both but not scrolled to the end.

So it basicaly crashes if you remove more elements than the left behind the current item (and you changed any). This means, if you scroll to the end and remove one, or if you scroll to (end-1) and remove two and so on.

Steps to Reproduce

  1. Create and carouselView bound to collection view that contains 3 items - A,B,C
  2. Scroll to the end of the carousel
  3. Trigger an method (by button) that changes the second item from B to D and removes the first item

Expected Behavior

A is removed, B is changed to D C remains as it is, and remains currentItem

Actual Behavior

App crash. System.ArgumentOutOfRangeException

Basic Information

Android simulator - Android 10.0 API 29 Xamarin.Forms 5.0.0.2012

Workarround

The workaround that I realy don't like to use is add an small delay between the update and delete. This probably gives the carousel time enough to recalculate something, so it doesn't crash.

PureWeen commented 3 years ago

@nziosys can you attach a small project reproducing the issue please?

nziosys commented 3 years ago

@PureWeen I attached very simple example. CarouselTest.zip

While creating it I found out some very crazy behaviour, that makes me doubt logic and computers at all ^^

`// this fails UpdateCollectionCommand = new Command(() => { Items[1] = "Item 4"; Items.RemoveAt(0); });

// this works UpdateCollectionCommand = new Command(() => { var toEdit = Items[1]; toEdit = "Item 4"; Items.RemoveAt(0); });`

Best regards