[MAUI .NET 6.0] CollectionView with ObservableCollection is throwing unhandled exception

I'm seeing an issue in my simple MAUI application with an ObservableCollection<string> inside a CollectionView. I can add items, but once I remove an item and then try to add another item I get the UnhandledException:
[Microsoft.UI.Xaml.UnhandledExceptionEventArgs] Exception = {"Value does not fall within the expected range."} Message = {"The parameter is incorrect."}
I have defensive code in each add/remove method to make sure the collection is not null and the item has content before removing or adding.
I'm sure this must have something to do with the binding, but the debugger does not have any stack trace info to indicate where the exact cause is.
I have also tried making sure the addition/deletion call is on the UI thread to no avail. I have also tried creating a temp list to copy the changes to and then copy back to the bound ObservableCollection, but the UI fails to update when I tried that.
[UPDATE]
I just discovered the issue was with the SwipeView inside the CollectionView. I hope someone on the MAUI team is working on the bugs with the SwipeView control.
I have taken it out for now; not sure how a developer would make this work on a phone?... for now I'm just going to show a delete button.
Updated:
<CollectionView
Grid.Row="2"
Grid.ColumnSpan="2"
ItemsSource="{Binding Items}"
SelectionMode="None">
<CollectionView.ItemTemplate>
<DataTemplate x:DataType="{x:Type x:String}">
<!-- [I had to remove the SwipeView because of it's buggyness and the UnhandledException: "Value does not fall within the expected range."]
<SwipeView>
<SwipeView.RightItems>
<SwipeItems>
<SwipeItem
BackgroundColor="Red"
Command="{Binding Source={RelativeSource AncestorType={x:Type viewmodel:MainViewModel}}, Path=DeleteCommand}"
CommandParameter="{Binding .}"
Text="Delete" />
</SwipeItems>
</SwipeView.RightItems>
-->
<Grid Padding="0,5" ColumnDefinitions=".9*, .1*">
<Frame Grid.Column="0" HasShadow="True">
<Label
Grid.Row="0"
Margin="0"
Padding="0"
FontSize="24"
Text="{Binding .}"
VerticalOptions="Start" />
</Frame>
<Button
Grid.Column="1"
Margin="3,0,0,0"
Padding="24"
BackgroundColor="Transparent"
Command="{Binding Source={RelativeSource AncestorType={x:Type viewmodel:MainViewModel}}, Path=DeleteCommand}"
CommandParameter="{Binding .}" />
</Grid>
<!--
</SwipeView>
-->
</DataTemplate>
</CollectionView.ItemTemplate>
</CollectionView>
Items is an ObservableCollection{string} inside the ViewModel.
You can uncomment the SwipeView to see the exception happen.
I've tested your code, and met the same exception.
I noticed there is a same issue as you in the GitHub: SwipeView throw exception on windows, and you have posted comments in it.
Please keep tracking this issue, our product group will fix MAUI moving forward.
In addition, I've integrated your code into question, due to it's not an answer.
I have already commented on that github thread, but thanks.
It's my pleasure to assist you.:)
Sign in to comment