Allan Jiang
Allan Jiang

Reputation: 11351

Windows Phone Silverlight page navigation

I have two questions here about Windows Phone page navigation:

  1. Is there a way to get the instance of the page I am navigating to? That is, if I am on page one and want to navigate to page2 to when button click, can I get the page2 instance after page2 is initialized by NavigationService.Navigate("page_2_uri") call in page 1?
  2. Is there a way I can know which page I navigate from? For example, I am currently on page3, and I want to do something like: if page 3 is navigated from page 2, I will do this, otherwise I will do that.

Thank you.

Upvotes: 0

Views: 510

Answers (2)

Claus Jørgensen
Claus Jørgensen

Reputation: 26345

  1. Is there a way to get the instance of the page I am navigating to?

No.

  1. Is there a way I can know which page I navigate from?

Yes. Traverse the NavigationService.BackStack

Upvotes: 3

Brian S
Brian S

Reputation: 5785

  1. The idea for using the NavigationService to navigate between pages is that you don't need to know any details about your destination. So in your example, Page 2 isn't initialized until you've left Page 1, and therefore Page 1 is no longer in scope, and won't be able to do anything with Page 2. If you want to pass information/context to Page 2, id recommend using Query Parameters (see next answer). If you want to know where the navigation is going, you can override the OnNavigatedFrom event and look at the Uri property of the NavigationEventArgs.

  2. I would recommend looking at the NavigationContext property of the Silverlight Page class. This property lets you view the QueryString of the navigation request. Using this approach, you could navigate to page 3 using a uri like "page_3?previous_page=2" and then extract the previous_page from the QueryString of the NavigationContext to see where you came from.

Upvotes: 2

Related Questions