简体   繁体   中英

WP7 Navigation with parameters

I have and app with 3 pages P1, P2 and P3.

When the app is navigating from P2 to P1 it pass a parameter.

On P1 I am getting the parameter value and I am showing a messagebox.

This is working perfectly, the following scenario is the problem:

P2 -> P1, Apps shows the message, P1->P3, P3->P1 using the back button and the app shows the message again with the parameter value from the P2, but it should not show any message.

This is the code of P1:

    protected override void OnNavigatedTo(NavigationEventArgs e)
    {
        base.OnNavigatedTo(e);
        String payment = "";
        if (NavigationContext.QueryString.TryGetValue("payment", out payment)) {

            if (payment == "no")
            {
                MessageBox.Show("Your payment failed!.",
               "Error", MessageBoxButton.OK);
            }
        }
    }

This is the code for P2:

NavigationService.Navigate(new Uri("/MainPage.xaml?payment=no", UriKind.Relative));

P3 does not pass any parameters

What is wrong why the app shows the message when navigating from P3 to P1?

Any help will be greatly appreciated.

You could try removing the key/value pair from the NavigationContext after you take it the first time

if (NavigationContext.QueryString.TryGetValue("payment", out payment)) {
    if (payment == "no")
        {
            MessageBox.Show("Your payment failed!.",
           "Error", MessageBoxButton.OK);
        }
        NavigationContext.QueryString.Remove("payment");
     }

Override the OnNavigatedTo event on your P1 and check if the back button was used like this:

protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)  
{  
    base.OnNavigatedTo(e);  

    //if the page transition is occurred by pressing back button.  
    if (e.NavigationMode == System.Windows.Navigation.NavigationMode.Back)  
    {  
        //do or do not do something  
    }  
} 

Try this, I hope it works.

String _oldUrl;
protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)
{
     String url = e.Uri.OriginalString;
     if(url!=_oldUrl)
     {
          //do work here

          _oldUrl = url;
     }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM