繁体   English   中英

带有参数的WP7导航

[英]WP7 Navigation with parameters

我有3页P1,P2和P3的应用程序。

当应用程序从P2导航到P1时,它将传递一个参数。

在P1上,我正在获取参数值,并且正在显示一个消息框。

这是完美的工作,以下情况是问题所在:

P2-> P1,应用程序使用后退按钮显示消息,P1-> P3,P3-> P1,并且应用程序使用P2中的参数值再次显示消息,但不应显示任何消息。

这是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);
            }
        }
    }

这是P2的代码:

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

P3没有传递任何参数

从P3导航到P1时,为什么该应用显示消息的原因何在?

任何帮助将不胜感激。

第一次使用后,您可以尝试从NavigationContext中删除键/值对

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

覆盖P1上的OnNavigatedTo事件,并检查是否使用了后退按钮,如下所示:

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

试试这个,我希望它能起作用。

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

          _oldUrl = url;
     }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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