Grouped ListView throws Index Out of Range exception when clicking outside of list in xamarin forms wpf

1.1k Views Asked by At

I have face issue with grouped listview in xamarin forms wpf application. I have used group listview in one of the pages in the application, where if I click outside of the grouped listview, the application is crashed. Please suggest any idea to fix this crash. Thank you.

exception : An unhandled exception of type 'System.ArgumentOutOfRangeException' occurred in mscorlib.dll Index was out of range. Must be non-negative and less than the size of the collection.

Image :

enter image description here

SampleCode :

Xaml :

<StackLayout HorizontalOptions = "FillAndExpand" VerticalOptions = "FillAndExpand">
<ListView x:Name="GroupedViewM" IsGroupingEnabled="true" ItemsSource="{Binding All}" HorizontalOptions="FillAndExpand" VerticalOptions="FillAndExpand" ItemTapped="SelectedUserorChannel">
                        <ListView.GroupHeaderTemplate>
                            <DataTemplate>
                                <ViewCell>
                                    <StackLayout Orientation="Horizontal" VerticalOptions="FillAndExpand" HorizontalOptions="FillAndExpand" Padding="0,0,0,10">
                                        <Label Text="{Binding Title}" TextColor="#e0e2e5" FontSize="22" VerticalTextAlignment="Center" VerticalOptions="FillAndExpand" HorizontalOptions="FillAndExpand"/>
                                        <Image x:Name="AddButton" IsVisible="{Binding AddChannelBtnVisibility}" Source="add.png" HeightRequest="20" WidthRequest="20" HorizontalOptions="EndAndExpand" VerticalOptions="CenterAndExpand" Margin="0,0,10,0">
                                            <Image.GestureRecognizers>
                                                <TapGestureRecognizer Tapped="CreateNewChannel"/>
                                            </Image.GestureRecognizers>
                                        </Image>
                                    </StackLayout>
                                </ViewCell>
                            </DataTemplate>
                        </ListView.GroupHeaderTemplate>
                        <ListView.ItemTemplate>
                            <DataTemplate>
                                <ViewCell>
                                    <StackLayout Orientation="Horizontal" HorizontalOptions="FillAndExpand" VerticalOptions="FillAndExpand">
                                        <Image Source="{Binding ProfileImage}" HorizontalOptions="Start" VerticalOptions="FillAndExpand" HeightRequest="20" WidthRequest="20"/>
                                        <Label Text="{Binding FirstName}" TextColor="#e0e2e5" FontSize="Small" HorizontalOptions="FillAndExpand" VerticalTextAlignment="Center" VerticalOptions="FillAndExpand"/>
                                        <Frame CornerRadius="5" BackgroundColor="#5e997c" Padding="8,0" IsVisible="{Binding MessageCountVisibility}" HorizontalOptions="EndAndExpand" VerticalOptions="CenterAndExpand">
                                            <Label Text="{Binding MessageCount}" TextColor="White" FontSize="15" HorizontalOptions="Center" VerticalOptions="Center"/>
                                        </Frame>
                                    </StackLayout>
                                </ViewCell>
                            </DataTemplate>
                        </ListView.ItemTemplate>
                    </ListView>

Xaml.cs

   private void SelectedUserorChannel(object sender, ItemTappedEventArgs e)
    {
        try
        {
            var userModel = ((ListView)sender).SelectedItem as UserModel;

            OpenUserOrGroupChat(userModel);

            ((ListView)sender).SelectedItem = null;
        }
        catch (Exception exception)
        {
            LoggingManager.Error(exception);
        }
    }

    private async void CreateNewChannel(object sender, EventArgs e)
    {
        try
        {
            await Helper.NavigateToAsync(new CreateChannelView());
        }
        catch (Exception exception)
        {
            LoggingManager.Error(exception);
        }
    }`
2

There are 2 best solutions below

5
On

Try this setting Footer property of this ListView as:

<ListView x:Name="GroupedViewM" IsGroupingEnabled="true" 
          ItemsSource="{Binding All}" 
          HorizontalOptions="FillAndExpand" 
          VerticalOptions="FillAndExpand" 
          ItemTapped="SelectedUserorChannel" 
          Footer="">
</ListView>
8
On

Probably you are casting null with some datatype say UserModel. i would suggest you to refactor your code with a null check in place

{
    if (((ListView)sender).SelectedItem != null && ((ListView)sender).SelectedItem as UserModel userModel)
    {
        OpenUserOrGroupChat(userModel);
        ((ListView)sender).SelectedItem = null;
    }
}