导航到Windows Phone 8中的同一页面

Mah*_*der 4 c# navigation windows-phone-8

在我的WP8应用程序中,我有一种情况,我必须从一个页面导航到另一个页面,然后我需要重新加载相同的页面由于某些原因.

MainPage.xaml - > Page1.xaml - > Page1.xaml - > Page1.xaml

当用户按下反键时,应返回"MainPage.xaml"页面.

我尝试使用NavigationService.navigate()导航,因为我无法重新加载页面.如果我使用导航网址传递任何唯一的查询字符串(例如:Guid),我可以重新加载页面.但是,当我按下后退按钮时 - 它永远不会返回到Mainpage.xaml页面.

有没有最好的方法来实现这一目标?

key*_*rdP 5

每次重新加载页面时都传入查询字符串(例如随机GUID).在您的OnNavigatedTo方法上检查GUID查询字符串是否存在.如果它确实存在,您知道您不希望这个页面,Navigation Stack因为它是重新加载的版本,因此您可以通过调用NavigationService.RemoveBackEntry来删除它.

protected override void OnNavigatedTo(NavigationEventArgs e)
{
    string guid = string.Empty;
    if (NavigationContext.QueryString.TryGetValue("guid", out guid))
    {
       //guid exists therefore it's a reload, so delete the last entry
       //from the navigation stack
       if(NavigationService.CanGoBack)
          NavigationService.RemoveBackEntry();
    }
}
Run Code Online (Sandbox Code Playgroud)