WP7 Navegación con parámetros

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?

Cualquier ayuda será apreciada.

preguntado el 28 de agosto de 12 a las 13:08

3 Respuestas

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

Respondido 28 ago 12, 13:08

Anular el 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  
    }  
} 

Respondido 28 ago 12, 13:08

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

Respondido 28 ago 12, 13:08

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.