当我在导航Xamarin Forms中传递值时,对象不匹配



I Try To Navigate from ListView Page列表视图从Sqlit返回数据当我从ListView中选择时,第一页中的代码如下:Xaml代码:

<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
         xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
         x:Class="FirstTestApp.AllTripsPage"
         Title="AllTrips">
<ContentPage.Content>
    <StackLayout Orientation="Vertical">
        <ListView x:Name="TripsList" ItemSelected="TripsList_ItemSelected">
        <ListView.ItemTemplate>
            <DataTemplate>
                <ViewCell>
                    <StackLayout Orientation="Horizontal" Padding="5,5,5,5">
                        <Label Text="{Binding Name}" FontSize="Medium" />
                       </StackLayout>
                </ViewCell>
            </DataTemplate>
        </ListView.ItemTemplate>
    </ListView>
    </StackLayout>
</ContentPage.Content>

C#函数:

private async void TripsList_ItemSelected(object sender, SelectedItemChangedEventArgs e)
    {
        if (e.SelectedItem == null)
        {
            return;
            //ItemSelected is called on deselection, 
            //which results in SelectedItem being set to null
        }
        var Selected = (Trip)e.SelectedItem;
      await  Navigation.PushAsync(new ShowTrip(Selected));
    }

该代码在页面ShowTrip中,它查看有关项目I选择的一些详细信息:xaml代码:

<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
         xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
         x:Class="FirstTestApp.ShowTrip"
         Title="Trip Details">
<ContentView.Content>
    <Label Text="Trip Details" />
        <Label Text="Name" />
        <Label  Text ="{Binding Name}" />
        <Label  Text="Description" />
        <Label  Text ="{Binding Description}"/>
        <Label  Text="Done:" />
    <Label  Text ="{Binding Done}"/>
</ContentView.Content>

C#:

public partial class ShowTrip : ContentPage
{
    Trip SelectTrip;
    public ShowTrip(Trip Selected)
    {
        InitializeComponent();
        SelectTrip = Selected;
        BindingContext = SelectTrip;
    }
}

InitializeComponent()函数中出现错误:

 private void InitializeComponent() {
        this.LoadFromXaml(typeof(ShowTrip));///in This Line Exception happened 
    }

错误为:"对象与目标类型不匹配"

ShowTrip页面上尝试使用StackLayout而不是<ContentView.Content>

相关内容

最新更新