0

I pass a value to a page

NavigationService.Navigate("/SportsPage.xaml?Number=5",UriKind.Relative);

the Number value changes while user is in the page, it gets 10 for example. then he goes to another page, but after a back navigation from that page to this page, the number is still 5. like it is the first time. but I want it to be the number when the user left the page (i.e 10).

I can save it to the storage and retrieve it on navigationTo, but it is not the case.

Is it possible to return back to the page in its last state?

4

2 回答 2

2

当您按下后退按钮时,页面将返回到其最后一个状态。问题是 OnNavigatedTo 事件在返回页面时会再次执行,因此您必须注意不要覆盖变量的值。

基本上,您的代码类似于:

protected virtual void OnNavigatedTo(NavigationEventArgs e)
{
    this.Number = this.NavigationContext.QueryString["Number"];
}

您应该将其更改为:

protected virtual void OnNavigatedTo(NavigationEventArgs e)
{
    if (e.NavigationMode != System.Windows.Navigation.NavigationMode.Back)
    {
         this.Number = this.NavigationContext.QueryString["Number"];
    }
}

这样,您在返回页面时不会覆盖变量的最后一个值。

于 2013-08-02T11:47:02.243 回答
0

您可以像这样在 App.xaml.cs 中创建一个全局变量:

public int Number { get; set; }.

并使用它进行操作:

(App.Current as App).Number = 5;
于 2013-08-02T11:47:54.277 回答