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

Steve Chamlee 1 Reputation point
2022-08-17T21:07:26.16+00:00

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.

.NET MAUI
.NET MAUI
A Microsoft open-source framework for building native device applications spanning mobile, tablet, and desktop.
2,853 questions
{count} votes