我有这个XAML ListViewItem:
<ListView HorizontalAlignment="Center" Height="500" Margin="0,130,0,0"
VerticalAlignment="Top" Width="400" x:Name="RoutesListView"
SelectionChanged="RoutesListView_SelectionChanged"
IsItemClickEnabled="True" SelectionMode="Single"
ItemClick="RoutesListView_ItemClick">
<ListView.ItemTemplate>
<DataTemplate>
<StackPanel >
<TextBlock Text="{Binding RouteName}"
Margin="20,0,20,8"
FontFamily="Calibri"
FontSize="24
FontWeight="SemiBold"
Foreground="Gray"
/>
</StackPanel>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
在点击处理程序中,我想将Route对象的ID发送到这样的新页面:
private void RoutesListView_ItemClick(object sender, ItemClickEventArgs e)
{
var items = (sender as ListView).Items;
var route = items[index] as Route;
var id = route.ID;
var data = new RouteParameters { ID = id };
Frame.Navigate(typeof(RouteView), data);
}
RouteParameters类如下所示:
public class RouteParameters
{
public int ID { get; set; }
}
最后在RouteView中我覆盖OnNavigatedTo,如下所示:
protected override void OnNavigatedTo(NavigationEventArgs e)
{
var p = e.Parameter as RouteParameters;
int id = p.ID;
}
我在Frame.Navigate方法中得到Null异常。对不起,如果我错过了一些明显的东西,但是在调试每一行并且没有看到任何空对象之后我无法找出导致错误的原因
答案 0 :(得分:0)
好的,所以我发现我以前在RouteView类中添加了一个参数,并且在尝试将数据传递给新视图的不同方法时忘记删除它。这不再是问题,我将在48小时内关闭该线程。