【问题标题】:Navigation loop and clearing back-stack导航循环和清除回栈
【发布时间】:2012-03-01 08:18:30
【问题描述】:

现在,我所拥有的是,当用户点击页面时,页面会自动将用户发送到打开 pdf 文件的 webtask。

现在发生的情况是,当用户按下后退按钮时,它会在一瞬间返回原始页面,然后被重定向回我分配给它的 pdf(由于 onnavigateto 功能)

我该如何做到,当用户点击 pdf 文档中的后退按钮时,应用程序会将用户带回主页?

另外,在主页面,我如何确保后台清空? (由于应用程序必须在 MainPage 上退出,因此无法返回 pdf。)

到目前为止,我的代码,我已经尝试过......

{
public partial class Page2 : PhoneApplicationPage
{
public Page2()
{
    InitializeComponent();
}

//as soon as this page is opened, navigate/redirect it to the URL below

protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)
{
    base.OnNavigatedTo(e);

    WebBrowserTask task = new WebBrowserTask() { URL ="http://test.com/test.pdf"};
    task.Show();

}

//when the user clicks the hardware back button, instead of taking them to the daily notices,    which will send them back to brower
// send the user to the main page

protected override void OnBackKeyPress

              (System.ComponentModel.CancelEventArgs e)
{

    base.OnBackKeyPress(e);

    new Uri("/MainPage.xaml", UriKind.Relative);


}

【问题讨论】:

    标签: windows windows-phone-7 navigation


    【解决方案1】:

    首先,为什么您需要只打开WebBrowserTask 的第二页?您可以从主页执行此操作。

    如果您仍想从第二页打开,您可以将WebBrowserTask 移动到构造函数并用Dispatcher 包围它。这种方法保证WebBrowserTask在导航到该页面后只会被调用一次(可能会出现一些墓碑问题)。或者,您可以将状态保存到 PhoneApplicationPage.State 以处理用户所在的位置以及接下来应该打开的内容。

    为了清除堆栈,您可以使用下一个代码:

    while (NavigationService.BackStack.Any())
    {
       NavigationService.RemoveBackEntry();
    }
    

    【讨论】:

      【解决方案2】:

      您必须在应用程序级别而不是页面级别检测到这一点。当您将用户“重定向”到 PDF 时,您的应用程序将暂停。当他们然后导航回来时,它会恢复。

      Visual Studio 模板提供了一个在应用程序恢复时调用的方法:

      // Code to execute when the application is activated (brought to foreground)
      // This code will not execute when the application is first launched
      private void Application_Activated(object sender, ActivatedEventArgs e)
      {
      }
      

      在上述方法中,您可以设置一个标志,然后检查您的页面何时导航到,这表明已发生恢复。

      【讨论】:

        猜你喜欢
        • 2015-06-26
        • 1970-01-01
        • 2015-10-31
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-04-13
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多