首页 > 解决方案 > 当我从 Listview 中选择项目以打开子页面时,iPhone 中没有任何反应。一切都在 android 设备中正常工作

问题描述

当我从列表视图中选择任何项目时,应该打开子页面。触发 listview onselecteditem 事件,但未打开子页面。

我正在使用 Xamarin 表单版本:3.3.0.967583 Visual Studio 2017

这是我的 ListView 绑定的 XAML 页面代码

                    <ListView.ItemTemplate>
                        <DataTemplate>
                            <ViewCell>
                                <Grid Padding="8, 10 ,8, 5" BackgroundColor="#FFFFFF" HeightRequest="45" Margin="80, 0, 80, 0">
                                    <Grid.RowDefinitions>
                                        <RowDefinition Height="*" />
                                    </Grid.RowDefinitions>
                                    <Grid.ColumnDefinitions>
                                        <ColumnDefinition Width="40" />
                                        <ColumnDefinition Width="*" />
                                        <ColumnDefinition Width="50" />
                                    </Grid.ColumnDefinitions>
                                    <RelativeLayout Grid.Row="0" Grid.Column="0" HeightRequest="40" WidthRequest="40">
                                        <AbsoluteLayout HeightRequest="40" WidthRequest="40">
                                            <Frame CornerRadius="{OnPlatform Android='40', iOS='20'}" BackgroundColor="#A9A9A9" HasShadow="False" OutlineColor="Transparent" BorderColor="Transparent" HeightRequest="40" WidthRequest="40" Padding="0" Margin="0" AbsoluteLayout.LayoutBounds="0, 0, 1, 1" AbsoluteLayout.LayoutFlags="All">
                                                <Frame CornerRadius="{OnPlatform Android='40', iOS='20'}" BackgroundColor="#A9A9A9" HasShadow="False" OutlineColor="Transparent" BorderColor="Transparent" HeightRequest="40" WidthRequest="40" Padding="0" Margin="0">
                                                    <Image Source="Profile.png" Aspect="Fill" VerticalOptions="Center" HorizontalOptions="Center" BackgroundColor="Transparent" HeightRequest="40" WidthRequest="40" Margin="-1, -1, -1, -1"></Image>
                                                </Frame>
                                            </Frame>
                                        </AbsoluteLayout>
                                        <AbsoluteLayout HeightRequest="39" WidthRequest="39">
                                            <Label Text="&#xf111;" FontSize="10" TextColor="{Binding IsOnline}" 
                                            AbsoluteLayout.LayoutFlags="All"
                                            AbsoluteLayout.LayoutBounds="1, 1, 10, 10"
                                            VerticalOptions="End" HorizontalOptions="End" >
                                                <Label.FontFamily>
                                                    <OnPlatform x:TypeArguments="x:String"
                                                    Android="Font Awesome 5 Free-Solid-900.otf#Font Awesome 5 Free Solid" 
                                                    iOS="FontAwesome5FreeSolid" />
                                                </Label.FontFamily>
                                            </Label>
                                        </AbsoluteLayout>
                                    </RelativeLayout>
                                    <StackLayout Grid.Row="0" Grid.Column="1" VerticalOptions="CenterAndExpand" HeightRequest="30" Padding="2, 0, 0, 5">
                                        <Label x:Name="UserName" Text="{Binding UserName}" FontSize="14" x:Uid="{Binding RoomName}" HorizontalOptions="Start" FontAttributes="Bold" TextColor="#000000" LineHeight="0" MaxLines="1" Margin="{OnPlatform Android='0, -3, 0, 0', iOS='0, -3, 0, 0'}" LineBreakMode="TailTruncation" HeightRequest="18" />
                                        <Label Text="{Binding RequestStatus}" FontSize="12" HorizontalOptions="Start"  VerticalOptions="Start" TextColor="{StaticResource lightGray}" LineHeight="0" MaxLines="1" Margin="{OnPlatform Android='0, -5, 0, 0', iOS='0, -5, 0, 0'}" LineBreakMode="TailTruncation" HeightRequest="18" />
                                    </StackLayout>
                                    <StackLayout Grid.Row="0" Grid.Column="2" VerticalOptions="Start" HorizontalOptions="End">
                                        <Label FontSize="10" HorizontalOptions="End" TextColor="{Binding IsOnline}" Text="{Binding MessageTime}" LineHeight="1" Margin="{OnPlatform Android='0, 2, 0, 0', iOS='0, 0, 0, 0'}" />
                                        <Grid Padding="0" Margin="0">
                                            <Grid.RowDefinitions>
                                                <RowDefinition Height="30" />
                                            </Grid.RowDefinitions>
                                            <Grid.ColumnDefinitions>
                                                <ColumnDefinition Width="50" />
                                            </Grid.ColumnDefinitions>
                                            <Label Grid.Row="0" Grid.Column="0" Text="&#xf27a;" IsVisible="{Binding IsAcceptDisplay}" FontSize="14"
                                                    TextColor="#2FB26C" HeightRequest="30" VerticalOptions="Center" WidthRequest="30" HorizontalOptions="End" HorizontalTextAlignment="End">
                                                <Label.FontFamily>
                                                    <OnPlatform x:TypeArguments="x:String"
                                                    Android="Font Awesome 5 Free-Solid-900.otf#Font Awesome 5 Free Solid" 
                                                    iOS="FontAwesome5FreeSolid" />
                                                </Label.FontFamily>
                                            </Label>
                                            <StackLayout Grid.Row="0" Grid.Column="0" IsVisible="{Binding IsPendingMessageCount}" HorizontalOptions="End" Margin="14, 0, 0, 0">
                                                <Frame VerticalOptions="Center" HorizontalOptions="End" WidthRequest="16" MinimumWidthRequest="16" HeightRequest="{OnPlatform Android='16', iOS='15'}" 
                                                       OutlineColor="#2FB26C" BackgroundColor="#2FB26C" CornerRadius="{OnPlatform Android='20', iOS='8'}" 
                                                       Margin="0, 0, 0, 0" Padding="{OnPlatform Android='0', iOS='0, 1, 0, 0'}" HasShadow="False">
                                                    <Label Text="{Binding PendingMessageCount}" FontSize="{OnPlatform Android='8', iOS='7'}" FontAttributes="Bold" VerticalOptions="CenterAndExpand" VerticalTextAlignment="Center"
                                                    TextColor="#FFFFFF" HorizontalOptions="Center" HorizontalTextAlignment="Center" HeightRequest="16" LineHeight="0.8"></Label>
                                                </Frame>
                                            </StackLayout>
                                        </Grid>
                                    </StackLayout>
                                </Grid>
                            </ViewCell>
                        </DataTemplate>
                    </ListView.ItemTemplate>
                    <ListView.Footer>
                        <StackLayout Orientation="Horizontal"></StackLayout>
                    </ListView.Footer>
                </ListView>
            </StackLayout>
        </AbsoluteLayout>
    </StackLayout>

下面是我管理 OnItemSelected 事件的 XAML.CS 页面

 async void OnItemSelected(object sender, SelectedItemChangedEventArgs args)
    {
        try
        {
             var item = args.SelectedItem as User;

            if (item == null)
                return;

            App.chatBoxPage = null;
            App.chatBoxPage = new ChatBoxPage(new ChatViewModel(item));
            await Navigation.PushAsync(App.chatBoxPage);
            ChatList.SelectedItem = null;

        }
        catch (Exception e)
        {
            await DisplayAlert("Live Chat", "On Clicked  : " + e.ToString(), "OK");
        }
    }

有时会引发以下异常

 System.NullReferenceException: Object reference not set to an instance of an object
  at Xamarin.Forms.VisualElement.get_BackgroundColor () <0x10178c450 + 0x00028> in <81ae0eb290e24f558e192928e3e37a57#40b0903c179c66f2f42ecc37469bd36a>:0 
  at Xamarin.Forms.Platform.iOS.VisualElementRenderer`1[TElement].SetElement (TElement element) <0x1018a4620 + 0x000a3> in <85a5f025a15045539371e93897914a5b#40b0903c179c66f2f42ecc37469bd36a>:0 
  at Xamarin.Forms.Platform.iOS.VisualElementRenderer`1[TElement].Xamarin.Forms.Platform.iOS.IVisualElementRenderer.SetElement (Xamarin.Forms.VisualElement element) <0x1018a4020 + 0x0003f> in <85a5f025a15045539371e93897914a5b#40b0903c179c66f2f42ecc37469bd36a>:0 
  at Xamarin.Forms.Platform.iOS.Platform.CreateRenderer (Xamarin.Forms.VisualElement element) <0x10189cd70 + 0x0007b> in <85a5f025a15045539371e93897914a5b#40b0903c179c66f2f42ecc37469bd36a>:0 
  at Xamarin.Forms.Platform.iOS.VisualElementPackager.OnChildAdded (Xamarin.Forms.VisualElement view) <0x1018a2a70 + 0x000af> in <85a5f025a15045539371e93897914a5b#40b0903c179c66f2f42ecc37469bd36a>:0 
  at Xamarin.Forms.Platform.iOS.VisualElementPackager.Load () <0x1018a26e0 + 0x000ab> in <85a5f025a15045539371e93897914a5b#40b0903c179c66f2f42ecc37469bd36a>:0 
  at Xamarin.Forms.Platform.iOS.VisualElementRenderer`1[TElement].SetElement (TElement element) <0x1018a4620 + 0x003c7> in <85a5f025a15045539371e93897914a5b#40b0903c179c66f2f42ecc37469bd36a>:0 
  at Xamarin.Forms.Platform.iOS.VisualElementRenderer`1[TElement].Xamarin.Forms.Platform.iOS.IVisualElementRenderer.SetElement (Xamarin.Forms.VisualElement element) <0x1018a4020 + 0x0003f> in <85a5f025a15045539371e93897914a5b#40b0903c179c66f2f42ecc37469bd36a>:0 
  at Xamarin.Forms.Platform.iOS.Platform.CreateRenderer (Xamarin.Forms.VisualElement element) <0x10189cd70 + 0x0007b> in <85a5f025a15045539371e93897914a5b#40b0903c179c66f2f42ecc37469bd36a>:0 
  at Xamarin.Forms.Platform.iOS.ListViewRenderer+UnevenListViewDataSource.CalculateHeightForCell (UIKit.UITableView tableView, Xamarin.Forms.Cell cell) <0x1018f0fe0 + 0x000b7> in <85a5f025a15045539371e93897914a5b#40b0903c179c66f2f42ecc37469bd36a>:0 
  at Xamarin.Forms.Platform.iOS.ListViewRenderer+UnevenListViewDataSource.GetEstimatedRowHeight (UIKit.UITableView table) <0x1018f0af0 + 0x0021b> in <85a5f025a15045539371e93897914a5b#40b0903c179c66f2f42ecc37469bd36a>:0 
  at Xamarin.Forms.Platform.iOS.ListViewRenderer.UpdateEstimatedRowHeight () <0x1018c0970 + 0x0010f> in <85a5f025a15045539371e93897914a5b#40b0903c179c66f2f42ecc37469bd36a>:0 
  at Xamarin.Forms.Platform.iOS.ListViewRenderer.OnElementChanged (Xamarin.Forms.Platform.iOS.ElementChangedEventArgs`1[TElement] e) <0x1018bf1a0 + 0x00b3b> in <85a5f025a15045539371e93897914a5b#40b0903c179c66f2f42ecc37469bd36a>:0 
  at Xamarin.Forms.Platform.iOS.VisualElementRenderer`1[TElement].SetElement (TElement element) <0x1018a4620 + 0x004cb> in <85a5f025a15045539371e93897914a5b#40b0903c179c66f2f42ecc37469bd36a>:0 
  at Xamarin.Forms.Platform.iOS.VisualElementRenderer`1[TElement].Xamarin.Forms.Platform.iOS.IVisualElementRenderer.SetElement (Xamarin.Forms.VisualElement element) <0x1018a4020 + 0x0003f> in <85a5f025a15045539371e93897914a5b#40b0903c179c66f2f42ecc37469bd36a>:0 
  at Xamarin.Forms.Platform.iOS.Platform.CreateRenderer (Xamarin.Forms.VisualElement element) <0x10189cd70 + 0x0007b> in <85a5f025a15045539371e93897914a5b#40b0903c179c66f2f42ecc37469bd36a>:0 
  at Xamarin.Forms.Platform.iOS.VisualElementPackager.OnChildAdded (Xamarin.Forms.VisualElement view) <0x1018a2a70 + 0x000af> in <85a5f025a15045539371e93897914a5b#40b0903c179c66f2f42ecc37469bd36a>:0 
  at Xamarin.Forms.Platform.iOS.VisualElementPackager.Load () <0x1018a26e0 + 0x000ab> in <85a5f025a15045539371e93897914a5b#40b0903c179c66f2f42ecc37469bd36a>:0 
  at Xamarin.Forms.Platform.iOS.VisualElementRenderer`1[TElement].SetElement (TElement element) <0x1018a4620 + 0x003c7> in <85a5f025a15045539371e93897914a5b#40b0903c179c66f2f42ecc37469bd36a>:0 
  at Xamarin.Forms.Platform.iOS.VisualElementRenderer`1[TElement].Xamarin.Forms.Platform.iOS.IVisualElementRenderer.SetElement (Xamarin.Forms.VisualElement element) <0x1018a4020 + 0x0003f> in <85a5f025a15045539371e93897914a5b#40b0903c179c66f2f42ecc37469bd36a>:0 
  at Xamarin.Forms.Platform.iOS.Platform.CreateRenderer (Xamarin.Forms.VisualElement element) <0x10189cd70 + 0x0007b> in <85a5f025a15045539371e93897914a5b#40b0903c179c66f2f42ecc37469bd36a>:0 
  at Xamarin.Forms.Platform.iOS.VisualElementPackager.OnChildAdded (Xamarin.Forms.VisualElement view) <0x1018a2a70 + 0x000af> in <85a5f025a15045539371e93897914a5b#40b0903c179c66f2f42ecc37469bd36a>:0 
  at Xamarin.Forms.Platform.iOS.VisualElementPackager.Load () <0x1018a26e0 + 0x000ab> in <85a5f025a15045539371e93897914a5b#40b0903c179c66f2f42ecc37469bd36a>:0 
  at Xamarin.Forms.Platform.iOS.VisualElementRenderer`1[TElement].SetElement (TElement element) <0x1018a4620 + 0x003c7> in <85a5f025a15045539371e93897914a5b#40b0903c179c66f2f42ecc37469bd36a>:0 
  at Xamarin.Forms.Platform.iOS.VisualElementRenderer`1[TElement].Xamarin.Forms.Platform.iOS.IVisualElementRenderer.SetElement (Xamarin.Forms.VisualElement element) <0x1018a4020 + 0x0003f> in <85a5f025a15045539371e93897914a5b#40b0903c179c66f2f42ecc37469bd36a>:0 
  at Xamarin.Forms.Platform.iOS.Platform.CreateRenderer (Xamarin.Forms.VisualElement element) <0x10189cd70 + 0x0007b> in <85a5f025a15045539371e93897914a5b#40b0903c179c66f2f42ecc37469bd36a>:0 
  at Xamarin.Forms.Platform.iOS.VisualElementPackager.OnChildAdded (Xamarin.Forms.VisualElement view) <0x1018a2a70 + 0x000af> in <85a5f025a15045539371e93897914a5b#40b0903c179c66f2f42ecc37469bd36a>:0 
  at Xamarin.Forms.Platform.iOS.VisualElementPackager.Load () <0x1018a26e0 + 0x000ab> in <85a5f025a15045539371e93897914a5b#40b0903c179c66f2f42ecc37469bd36a>:0 
  at Xamarin.Forms.Platform.iOS.VisualElementRenderer`1[TElement].SetElement (TElement element) <0x1018a4620 + 0x003c7> in <85a5f025a15045539371e93897914a5b#40b0903c179c66f2f42ecc37469bd36a>:0 
  at Xamarin.Forms.Platform.iOS.VisualElementRenderer`1[TElement].Xamarin.Forms.Platform.iOS.IVisualElementRenderer.SetElement (Xamarin.Forms.VisualElement element) <0x1018a4020 + 0x0003f> in <85a5f025a15045539371e93897914a5b#40b0903c179c66f2f42ecc37469bd36a>:0 
  at Xamarin.Forms.Platform.iOS.Platform.CreateRenderer (Xamarin.Forms.VisualElement element) <0x10189cd70 + 0x0007b> in <85a5f025a15045539371e93897914a5b#40b0903c179c66f2f42ecc37469bd36a>:0 
  at Xamarin.Forms.Platform.iOS.VisualElementPackager.OnChildAdded (Xamarin.Forms.VisualElement view) <0x1018a2a70 + 0x000af> in <85a5f025a15045539371e93897914a5b#40b0903c179c66f2f42ecc37469bd36a>:0 
  at Xamarin.Forms.Platform.iOS.VisualElementPackager.Load () <0x1018a26e0 + 0x000ab> in <85a5f025a15045539371e93897914a5b#40b0903c179c66f2f42ecc37469bd36a>:0 
 at Xamarin.Forms.Platform.iOS.PageRenderer.ViewDidLoad () <0x1018c9b80 + 0x0034f> in <85a5f025a15045539371e93897914a5b#40b0903c179c66f2f42ecc37469bd36a>:0 
  at PSS_LiveChat.iOS.IosKeyboardFixPageRenderer.ViewDidLoad () <0x10089e900 + 0x0001b> in <39287061e96f454f92c45a96e90470c1#40b0903c179c66f2f42ecc37469bd36a>:0 
  at (wrapper managed-to-native) ObjCRuntime.Messaging.objc_msgSendSuper(intptr,intptr)
  at UIKit.UIViewController.get_View () <0x10149e8c0 + 0x0006f> in <bbd12705940f4bb29392519af87b535b#40b0903c179c66f2f42ecc37469bd36a>:0 
  at Xamarin.Forms.Platform.iOS.PageRenderer.get_NativeView () <0x1018c9630 + 0x00023> in <85a5f025a15045539371e93897914a5b#40b0903c179c66f2f42ecc37469bd36a>:0 
  at Xamarin.Forms.Platform.iOS.PageRenderer.SetElement (Xamarin.Forms.VisualElement element) <0x1018c9670 + 0x000ef> in <85a5f025a15045539371e93897914a5b#40b0903c179c66f2f42ecc37469bd36a>:0 
  at Xamarin.Forms.Platform.iOS.Platform.CreateRenderer (Xamarin.Forms.VisualElement element) <0x10189cd70 + 0x0007b> in<85a5f025a15045539371e93897914a5b#40b0903c179c66f2f42ecc37469bd36a>:0 
  at Xamarin.Forms.Platform.iOS.NavigationRenderer.CreateViewControllerForPage (Xamarin.Forms.Page page) <0x1018c5e10 + 0x00027> in <85a5f025a15045539371e93897914a5b#40b0903c179c66f2f42ecc37469bd36a>:0 
  at Xamarin.Forms.Platform.iOS.NavigationRenderer+<OnPushAsync>d__51.MoveNext () <0x1018f95a0 + 0x0007b> in <85a5f025a15045539371e93897914a5b#40b0903c179c66f2f42ecc37469bd36a>:0 
--- End of stack trace from previous location where exception was thrown ---
  at Xamarin.Forms.NavigationPage+<PushAsyncInner>d__84.MoveNext () <0x10174c7f0 + 0x0023f> in <81ae0eb290e24f558e192928e3e37a57#40b0903c179c66f2f42ecc37469bd36a>:0 
--- End of stack trace from previous location where exception was thrown ---
  at Xamarin.Forms.NavigationPage+<PushAsync>d__55.MoveNext () <0x10174b730 + 0x00523> in <81ae0eb290e24f558e192928e3e37a57#40b0903c179c66f2f42ecc37469bd36a>:0 
--- End of stack trace from previous location where exception was thrown ---
  at PSS_LiveChat.Views.LiveChatPage+<OnItemSelected>d__5.MoveNext () <0x100cfb210 + 0x0040f> in <3b5956d19f5f4bdc84324069625851ef#40b0903c179c66f2f42ecc37469bd36a>:

标签: c#xamarin.formsxamarin.forms.listview

解决方案


我觉得这样做的原因是您ItemSelected在应该使用事件时使用事件,因为如果您单击同一项目两次它不会ItemTapped触发,则只有在您选择新的项目时才会触发项目选择事件。Item

ListView应该看起来像这样:

<ListView ItemTapped = "ListView_ItemTapped".../>

您在 cs 文件中的 ItemTapped 事件将如下所示:

 private void ListView_ItemTapped(object sender, ItemTappedEventArgs e)
    {
        // Your code
    }

推荐阅读