0

我在 xamarin 表单 wpf 应用程序中遇到了分组列表视图的问题。我在应用程序的一个页面中使用了组列表视图,如果我在分组列表视图之外单击,应用程序将崩溃。请提出任何解决此崩溃的想法。谢谢你。

异常:在 mscorlib.dll 索引中发生的“System.ArgumentOutOfRangeException”类型的未处理异常超出范围。必须是非负数且小于集合的大小。

图片 :

在此处输入图像描述

示例代码:

xml:

<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);
        }
    }`
4

2 回答 2

0

试试这个FooterListView 的这个设置属性:

<ListView x:Name="GroupedViewM" IsGroupingEnabled="true" 
          ItemsSource="{Binding All}" 
          HorizontalOptions="FillAndExpand" 
          VerticalOptions="FillAndExpand" 
          ItemTapped="SelectedUserorChannel" 
          Footer="">
</ListView>
于 2019-02-19T09:59:48.963 回答
0

可能您正在使用某些数据类型(例如 UserModel)强制转换为 null。我建议您使用空检查来重构您的代码

{
    if (((ListView)sender).SelectedItem != null && ((ListView)sender).SelectedItem as UserModel userModel)
    {
        OpenUserOrGroupChat(userModel);
        ((ListView)sender).SelectedItem = null;
    }
}
于 2019-02-19T09:38:14.740 回答