当我在导航中传递值时对象不匹配,Xamarin Forms
Object does not match when i pass value in navigation, Xamarin Forms
我尝试从 ListView 页面导航列表视图 Return 来自 Sqlit 的数据
当我从 ListView select 时,第一页下面的代码:
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));
}
Page ShowTrip 中的此代码,它查看有关我选择的项目的一些详细信息:
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
}
错误是:"Object does not match target type"
尝试在 ShowTrip
页面上使用 StackLayout
而不是 <ContentView.Content>
。
我尝试从 ListView 页面导航列表视图 Return 来自 Sqlit 的数据 当我从 ListView select 时,第一页下面的代码: 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));
}
Page ShowTrip 中的此代码,它查看有关我选择的项目的一些详细信息: 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
}
错误是:"Object does not match target type"
尝试在 ShowTrip
页面上使用 StackLayout
而不是 <ContentView.Content>
。