我有一个应用程序,有三个页面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;
}
}