-
Notifications
You must be signed in to change notification settings - Fork 1.9k
[Bug] System.IndexOutOfRangeException: 'Can't set CarouselView to position -1. ItemsSource has X items.' #14952
Comments
Could you attach a small sample where reproduce the issue?. Thanks in advance. |
I will extract the the code to an empty project and see if i can recreate this issue this week. tbc. |
@jsuarezruiz I've created a working 'crash' demo. https://github.com/npostma/bug_reports-CarouselViewAndroidCrash [edit] |
Clearing the collection ( .Clear() ) in the example instead of re initializing results in the same crash. |
I believe I solved your problem. Put this in your carousel viewmodel: public void OnDisappearing() then put this in your caurselpage.xaml.cs protected override void OnDisappearing() viewModel.OnDisappearing(); ClearCarousel(); This should work I tested it. |
What trickery is this :-) It seems to work. How can the IsBusy bool have effect on the collection? |
@jsuarezruiz Is there a better way to handle this exception than the solution that @brettnguyen suggested? ( thanks for the suggestion @brettnguyen ) |
Hello, This is two Stacktraces that can help to debug the crash. To reproduce the crash :
It seems that CarouselView doesn't like having its data refreshed when not displayed. I created an Android CarouselViewRenderer and I catched the first exception in
After exiting the try/catch, another exception, that I didn't achieve to catch, crash the app :
|
@brettnguyen thanks for the workaround, worked for me! |
Just add follow code in caurselpage.xaml.cs will fix it, this is not related to IsBusy: protected override void OnDisappearing()
{
base.OnDisappearing();
ClearCarousel();
} |
@jsuarezruiz do you still need more info on this issue? |
Anything? |
Anything? This issue still exists and giving me trouble from time to time and need to do some funky workarrounds because the IsBusy = false / true; does not alway help. |
This bug produces 3 stacktraces protected override void UpdateAdapter()
{
try
{
base.UpdateAdapter();
}
catch (ArgumentOutOfRangeException e)
{
System.Diagnostics.Debug.WriteLine(e));
}
} produces this
This protected override void SetUpNewElement(ItemsView newElement)
{
try
{
base.SetUpNewElement(newElement);
}
catch (ArgumentOutOfRangeException e)
{
System.Diagnostics.Debug.WriteLine(e);
}
} produces this
This protected override void OnElementPropertyChanged(object sender, PropertyChangedEventArgs changedProperty)
{
try
{
base.OnElementPropertyChanged(sender, changedProperty);
}
catch (IndexOutOfRangeException e)
{
System.Diagnostics.Debug.WriteLine(e);
}
} produces this
I think this mainly happens because the |
The solution is to set the the carroussel in a view model. |
Description
After updating the ObservableCollection which is bound to CarouselView, rendering the page with the carousel view gives a crash. Android only. iOS works as expected.
Exception message: [Bug] System.IndexOutOfRangeException: 'Can't set CarouselView to position -1. ItemsSource has X items.'
X can be any positive number, its the count of the updated ObservableCollection.
Its a Xamarin exception. I do not call the Position property.
Steps to Reproduce
3.a .Clear() or
3.b collection = new ObservableCollection();
I do not get a stack trace. So i have no clue where it originates.
Expected Behavior
That the app does not crash. Like it does in iOS. And the carousel shows the updated collection.
Actual Behavior
Basic Information
Environment
Code
My .XAML
Screenshots
N.a.
Workaround
Sadly no. No working workaround so far. Tried all kinds of things (like building it programmatically) but run into another issue where the carousel would not center and was always on top of the screen spite of being a 1:1 copy of the .XAML.
The text was updated successfully, but these errors were encountered: