Windows Phone 8.1 通用应用程序在从第二页返回时终

Windows Phone 8.1 Universal App terminates on navigating back from second page?(Windows Phone 8.1 通用应用程序在从第二页返回时终止?)
本文介绍了Windows Phone 8.1 通用应用程序在从第二页返回时终止?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的 Windows Phone 8.1 通用应用中有 2 个页面.

I have 2 pages in my Windows Phone 8.1 Universal App.

我使用带有点击事件代码的按钮从 Page1.xaml 导航到 Page2.xaml:

I navigate from Page1.xaml to Page2.xaml by using a button with the click event code:

this.Frame.Navigate(typeof(Page2));

当我在第 2 页上时,我使用硬件后退按钮,应用程序会毫无例外地关闭.它只是返回到开始屏幕.

When I am on Page2, and I use the hardware back button the app closes without an exception or anything. It just returns to the startscreen.

我已经在 第 2 页尝试了以下操作:

I already tried the following on Page 2:

public Page2()
    {
        this.InitializeComponent();
        Windows.Phone.UI.Input.HardwareButtons.BackPressed += HardwareButtons_BackPressed;
    }

    void HardwareButtons_BackPressed(object sender, Windows.Phone.UI.Input.BackPressedEventArgs e)
    {
        Frame.GoBack();
    }

据我所知,我没有清除后台堆栈.

As far as I know I do not clear the back stack.

发生了什么,我该如何解决?

What is going on, and how can I fix this?

亲切的问候,尼尔斯

推荐答案

这是 Windows Phone 8.1 的新功能.

This is new to Windows Phone 8.1.

如果您使用 VS2013 模板创建新的 Hub 通用应用程序,您会注意到 Common 文件夹中有一个名为 NavigationHelper 的类.

If you create a new Hub Universal App using a VS2013 template, you'll notice a class in Common folder called a NavigationHelper.

此 NavigationHelper 会提示您如何正确响应按下后退按钮.因此,如果您不想使用 NavigationHelper,以下是如何恢复旧行为:

This NavigationHelper gives you a hint how to properly react to back button press. So, if you don't want to use the NavigationHelper, here's how to get the old behavior back:

public BlankPage1()
{
    this.InitializeComponent();
    HardwareButtons.BackPressed += HardwareButtons_BackPressed;
}

void HardwareButtons_BackPressed(object sender, BackPressedEventArgs e)
{
    if (Frame.CanGoBack)
    {
        e.Handled = true;
        Frame.GoBack();
    }
}

您也可以在应用级别执行此操作,以避免在每个页面上执行此操作:

You can also do it on app level, to avoid having to do it on every page:

public App()
{
    this.InitializeComponent();
    this.Suspending += this.OnSuspending;

    #if WINDOWS_PHONE_APP
    HardwareButtons.BackPressed += HardwareButtons_BackPressed;
    #endif
}

#if WINDOWS_PHONE_APP
void HardwareButtons_BackPressed(object sender, BackPressedEventArgs e)
{
    Frame rootFrame = Window.Current.Content as Frame;

    if (rootFrame != null && rootFrame.CanGoBack)
    {
        e.Handled = true;
        rootFrame.GoBack();
    }
}
#endif

这篇关于Windows Phone 8.1 通用应用程序在从第二页返回时终止?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

DispatcherQueue null when trying to update Ui property in ViewModel(尝试更新ViewModel中的Ui属性时DispatcherQueue为空)
Drawing over all windows on multiple monitors(在多个监视器上绘制所有窗口)
Programmatically show the desktop(以编程方式显示桌面)
c# Generic Setlt;Tgt; implementation to access objects by type(按类型访问对象的C#泛型集实现)
InvalidOperationException When using Context Injection in ASP.Net Core(在ASP.NET核心中使用上下文注入时发生InvalidOperationException)
LINQ many-to-many relationship, how to write a correct WHERE clause?(LINQ多对多关系,如何写一个正确的WHERE子句?)