我怎样才能把一个框架的历史页面?(How can I remove pages from a Fra

2019-09-18 20:40发布

How can I manipulate a Frame's history in a WinRT XAML app?

The user will start on my hub page, where they can select an existing project to go to its edit screen, or they can select "new project". "New project" will take them through a short wizard, then take them to the "edit project" screen.

It makes sense for the wizard pages to just be pages that I navigate to in the frame; that way the user can back out of the wizard if they change their mind. (It'll only be two pages, so "back" can take the place of "cancel".) But once the wizard is done and the changes are committed, there's no longer any reason for those wizard pages to be in the history; if the user clicks Back from the "edit project" page, I want them to go right back to the hub.

To illustrate, I want the flow to look something like this:

  • Frame history: Hub. User clicks "New Project".
  • Frame history: Hub -> Wizard Page 1. User clicks "Next".
  • Frame history: Hub -> Wizard Page 1 -> Wizard Page 2. User clicks "Finish".
  • Frame history: Hub -> Edit Project.

Frame doesn't seem to have any methods along the lines of "remove from history". The docs do have hints that there might be some way to override the history, because the docs for GoBack say "Navigates to the most recent item in back navigation history, if a Frame manages its own navigation history" (emphasis mine), but that's all it has to say on the topic -- there's no mention of how someone else can manage history for it. So I don't know whether that's useful or not.

How can I remove my wizard pages from my Frame's history once the user completes the wizard?

Answer 1:

您可以通过调用框架SetNavigationState(字符串navigationState)请从历史记录页面。 不幸的是,系列化navigationState的格式是“仅供内部使用”,所以只是改变了字符串可能破坏你的代码在将来的版本。

我只能想到一个面向未来的方法来彻底清除导航堆栈:

  1. 在程序启动时调用GetNavigationState保存空导航状态。
  2. 在调用浏览您的编辑项目页面,请拨打SetNavigationState与空导航状态。

您的编辑项目页面现在会在堆栈上的第一页。



Answer 2:

与Windows 8.1开始,你有机会获得一帧的堆栈中财产。 您可以轻松地删除某些内容或清除整个背部堆栈。

下面是我用的是什么明显的后退堆栈:

var rootFrame   = (Window.Current.Content as Frame);
rootFrame.Navigate(typeof(MyPage));
rootFrame.BackStack.Clear();


Answer 3:

它为我的解决方案:

while (ContentFrame.BackStack.Count > 1)
            {
              ContentFrame.BackStack.Remove(ContentFrame.BackStack.ElementAt(ContentFrame.BackStack.Count-1));

            }


文章来源: How can I remove pages from a Frame's history?