For Windows Phone 7 (WP7), develop "Back button" and "process querystring in onnavigationto"

Source: Internet
Author: User

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 ).

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.