0

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);
        }
    }`
Deepakkumar
  • 86
  • 1
  • 10
  • Is there any code associated? Do you handle the clicked event yourself? Please update your question with relevant code. – Gerald Versluis Feb 19 '19 at 09:10
  • Hi, I have updated the question with code that I am using. Please look at once. – Deepakkumar Feb 19 '19 at 09:25
  • @Deepakkumar Are you sure, this is because of the list view, meaning the area highlighted is the list view and not any other hidden item? Your XAML seems to be correct, what elements are there other than the Listview on this page? Can you also share your code for OpenUserOrGroupChat(userModel); method? – Hardik Mistry Feb 19 '19 at 11:56
  • Hi, There are no hidden items in view. When I run above code in android it's work fine, I have faced this issue only when I run in WPF platform. – Deepakkumar Feb 19 '19 at 12:00
  • Need at least part of the stack trace. To determine if the crash is happening inside Xamarin.Forms code. For example, if you were on Android, stack trace might include a line like `Xamarin.Forms.Platform.Android.ListViewRenderer.OnLayout`, indicating that it ran into trouble filling the listview. I don't know what the equivalent is on WPF. – ToolmakerSteve Mar 23 '19 at 13:16
  • If you change your code so that the listview is NOT empty - has at least one item - does the crash still happen? – ToolmakerSteve Mar 23 '19 at 13:18
  • If you *remove* the listview, then run again, does the page still crash when clicked? If so, does the error message change? Also, you mentioned that this does not crash on Android - were there any *warnings* in the Android Output pane, that might be related to this? That is, perhaps there was a problem also on Android, but Android recovered without crashing. – ToolmakerSteve Mar 23 '19 at 13:23

2 Answers2

0

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;
    }
}
  • Thank you for your response. I have tried as you suggest but still, the application crashed. – Deepakkumar Feb 19 '19 at 09:40
  • can you share the crash report or stack trace? – Aditya Deshpande Feb 20 '19 at 06:43
  • Below exception, I have got in the output window. ex : 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. – Deepakkumar Feb 20 '19 at 07:27
  • So you get ArgumentOutOfRangeException The error means you cannot add enough subitems to your Listview. If you only add three subitems to your listview, you cannot use the code lvnames.Items[x].SubItems[4].Text, the indexes are 0, 1, 2,3. Please putting a breakpoint in the code and see what your data looks like. See how many subitems the lvnames have. And the number of columns in your ListView irrelevant to the number of subitems in Listview. ref: http://msdn.microsoft.com/en-us/library/system.windows.forms.listviewitem.subitems(v=vs.110).aspx – Aditya Deshpande Feb 21 '19 at 04:51
  • put a breakpoint at the if statement and check what is getting null or getting out of range. – Aditya Deshpande Feb 21 '19 at 04:53
  • I have tried but breakpoint not raised at any line of code, directly application is crashed. – Deepakkumar Feb 21 '19 at 09:12
  • Can you also share the part of code that is relevant to this list view and data binding? the code that you have in your viewmodel – Aditya Deshpande Feb 21 '19 at 11:55
  • Casting null *never* crashes; null is a valid value for any reference type, so the cast succeeds. The crash would occur when attempting to *access a field* of that null value. So your proposed test will not help. – ToolmakerSteve Mar 23 '19 at 13:21
0

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>
Himanshu Dwivedi
  • 7,934
  • 3
  • 31
  • 52