导航页面时 xaml 中的 InvalidCastException
InvalidCastException in xaml while navigate the page
这是MainPage的点击事件代码
void ItemView_ItemClick(object sender, ItemClickEventArgs e)
{
var item = ((EventItem)e.ClickedItem);
this.Frame.Navigate(typeof(EventPage), new Navigator() { Parent = "Dashboard", Event = item });
}
加载事件的sender
参数是引发事件的控件(本例中为当前页面);所以它的类型是页面的类型,而不是 Navigator
.
显然您正在尝试访问传递给 Frame.Navigate
的参数。为此,您应该覆盖 OnNavigatedTo
方法:
protected override void OnNavigatedTo(NavigationEventArgs e)
{
var param = (Navigator)e.Parameter;
...
}
这是MainPage的点击事件代码
void ItemView_ItemClick(object sender, ItemClickEventArgs e)
{
var item = ((EventItem)e.ClickedItem);
this.Frame.Navigate(typeof(EventPage), new Navigator() { Parent = "Dashboard", Event = item });
}
加载事件的sender
参数是引发事件的控件(本例中为当前页面);所以它的类型是页面的类型,而不是 Navigator
.
显然您正在尝试访问传递给 Frame.Navigate
的参数。为此,您应该覆盖 OnNavigatedTo
方法:
protected override void OnNavigatedTo(NavigationEventArgs e)
{
var param = (Navigator)e.Parameter;
...
}