Let's take a look at a simple figure:
Page jump in the program is not normal, but in the WP7 program, we may need to consider the "trouble" caused by the back button ".
In this page structure in, assume that there is the following code in page2.xaml. CS:
protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)
{
base.OnNavigatedTo(e);
if (NavigationContext.QueryString.ContainsKey("id"))
{
string id= NavigationContext.QueryString["id"];
if (id=="1")
NavigationService.Navigate(new Uri("page3.xaml",UriKind.Relative));
}
}
Although this logic is very strange, why does it jump to page3 when id = 1? Oh, that is not the focus of our discussion.
Run the program, the phenomenon is as follows: From page1 click a button and pass id = 1 to page2, then page2 directly jumps to page3. if you want to back up now, press the "back" button in the lower left corner of the phone, question! When the screen flashed, it went back to page3. the principle was very simple, because page2 judges whether the URL contains the ID parameter every time onnavigatedto. If yes, it jumps to page3. Unfortunately, in page3, press the back key. The URL is still a URL with parameters.
To solve this problem, add a line of code to solve it:
protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)
{
base.OnNavigatedTo(e);
if (NavigationContext.QueryString.ContainsKey("id"))
{
string id= NavigationContext.QueryString["id"];
if (id=="1")
{
NavigationContext.QueryString.Remove("id");
NavigationService.Navigate(new Uri("page3.xaml",UriKind.Relative));
}
}
}
Navigationcontext. querystring. Remove ("ID"); this statement can remove a parameter from the current URL. In this way, the page2 will be rolled back in page3 to stop page2.
Navigationcontext. querystring. remove ("") Actual use case: to prevent onnavigatedto from repeatedly handling the same problem, as well as the page Jump situation mentioned above (of course, you can also use other methods to avoid this complicated jump ).