iCobot
iCobot

Reputation: 175

Windows Phone 8 Navigate back to same page

Does anyone know if the NavigationService.Navigate has any restrictions or does not call a pages default constructor when trying to navigate back to itself? So lets say I am on MainPage.xaml and I do some stuff and want to redirect back to itself using NavigationService.Navigate(new Uri("/MainPage.xaml", UriKind.RelativeOrAbsolute));. I have that now and put a break point in the default constructor and it never gets hit when I try to navigate the page back to itself.

EDIT: From my testing it seems like it caches itself. I tested this theory by putting a querystring variable to my navigation, "/MainPage.xaml?z=" + DateTime.Now.ToLocalTime() and it hit the constructor that way but not by just navigating back to just "MainPage.xaml".

Upvotes: 2

Views: 1259

Answers (3)

Mani
Mani

Reputation: 1374

If you are using NavigationService.navigate(...), it will create a new page and the constructor will be called because it is creating a new object but if the call is made like

NavigationService.goback()

it will go back and call the onNavigated() method.

To explain it more clearly you can think that you have three pages A,B,C and on A you used navigate(to B) and this will create B page. Let us assume that you used another navigate method and this will create another page C. Now if you use

  • navigate(to A) method, the page stack will be A-B-C-A and goBack() will pop pages from the stack

  • goBack() method, the page stack will be A-B and another goback() will pop another page resulting in only A page.

A constructor is called only once when it is created and the rest of times only onNavigated() method will be called.

Upvotes: 2

Rajeev Nair
Rajeev Nair

Reputation: 759

Do you really want to hit the Constructor? There are alternatives. I do not do anything in the constructor. All I do is bind a event handler for Load and do all the initialization in the Load event handler. Also, there is OnNavigateTo() which gets hit when you use NavigationService.Navigate().

If you have OnNavigateTo and Load in one code-behind, OnNavigateTo gets hit first and then the Load.

I hope I answered your question.

Upvotes: 1

Jaihind
Jaihind

Reputation: 2778

This may help you.

NavigationService.Navigate(new Uri(string.Format("/MainPage.xaml?Refresh=true&random={0}", Guid.NewGuid()), UriKind.Relative));

Upvotes: 2

Related Questions