Search code examples
windowsxamlexceptionwindows-mobile

InvalidCastException in xaml while navigate the page


This is code of click event of MainPage

void ItemView_ItemClick(object sender, ItemClickEventArgs e)
{
    var item = ((EventItem)e.ClickedItem);
    this.Frame.Navigate(typeof(EventPage), new Navigator() { Parent = "Dashboard", Event = item });
}

In image the code of next page


Solution

  • The sender parameter of the loaded event is the control that raised the event (in this case, the current page); so its type is the type of the page, not Navigator.

    Apparently you're trying to access the argument that was passed to Frame.Navigate. To do that, you should override the OnNavigatedTo method:

    protected override void OnNavigatedTo(NavigationEventArgs e)
    {
        var param = (Navigator)e.Parameter;
        ...
    }