0

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.

3 Answers 3

2

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");
     }
1

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  
    }  
} 
0

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;
     }
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.