Wizard
Wizard

Reputation: 1162

How to navigate to another Page from a User Control in Windows Phone 8.1

I have searched for this, but wasn't able to find anything directly related to the Windows Phone 8.1 Environment, please let me know if there is a link available as I would have thought this would be a common question..

I am trying to navigate to another page from within a UserControl when an Item in a list is tapped, however, I am having troubling getting Parent page's Frame so I can navigate.

Essentially, I am trying to do this within the User Control.

 private void lstFoo_ItemClick(object sender, ItemClickEventArgs e)
    {
        Page parentPage = this.Parent as Page; //Returns null

        if (parentPage != null)
        {
            parentPage.Frame.Navigate(typeof(BarPage), e.ClickedItem);
        }
    }

The User Control will be used on multiple pages, and all with the same result, however I just can't work out how to get that Navigate to work and then pass the parameter along. Any help would be appreciated..

Thanks.

Upvotes: 5

Views: 1626

Answers (1)

Romasz
Romasz

Reputation: 29792

If you are using standard WP template with one Frame set as Content of your Window then it should be possible to do it like this:

private void lstFoo_ItemClick(object sender, ItemClickEventArgs e)
{
    (Window.Current.Content as Frame).Navigate(typeof(BarPage), e.ClickedItem);
}

Only look out with passing this parameter - if it's not serializable then when your app will be suspended then SuspensionManager will throw exception.

Upvotes: 6

Related Questions