問題描述
我的 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 通用應用程序在從第二頁返回時終止?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!