問題描述
在我的 Xaml 頁面中,我有一個框架.
In my Xaml Page I've got a Frame.
我正在嘗試讓 backButton 事件在框架內導航.
I'm trying to have a backButton event to just navigate inside frame .
所以我嘗試使用這段代碼
so I tried to use this piece of code
public MainPage(){
this.InitializeComponent();
if(Windows.Foundation.Metadata.ApiInformation.IsTypePresent("Windows.Phone.UI.Input.HardwareButtons")) {
Windows.Phone.UI.Input.HardwareButtons.BackPressed += HardwareButtons_BackPressed;
}
}
private void HardwareButtons_BackPressed(object sender,BackPressedEventArgs e) {
if(insideFrame.CanGoBack())insideFrame.GoBack();
else Application.Current.Exit();
}
但在手機中執行 HardwareButtons_BackPressed
事件后它會關閉應用程序.
but In phone after doing HardwareButtons_BackPressed
event it close the application.
似乎在 MainPage 上運行了一些默認的后退按鈕行為...
It seems to running some default back button behavior on MainPage...
我該如何解決?在 Windows10 中他們是否添加了新事件來處理返回導航?
How can I fix it? And In Windows10 does they add new events to handle back navigation?
[更新]
現在我發現在 Windows 10 中使用 SystemNavigationManager
比使用 Input.HardwareButtons.BackPressed
更好.
Now I found out it's better to Use SystemNavigationManager
in Windows 10 instead of Input.HardwareButtons.BackPressed
.
SystemNavigationManager currentView = SystemNavigationManager.GetForCurrentView();
推薦答案
您需要通過將 BackPressedEventArgs 的 Handled 屬性設置為 true 來告訴系統您處理了后退按鈕按下.
You need to tell the system that you handled the backbutton press by setting the Handled property of the BackPressedEventArgs to true.
private void OnHardwareButtonsBackPressed(object sender, BackPressedEventArgs e)
{
// This is the missing line!
e.Handled = true;
// Close the App if you are on the startpage
if (mMainFrame.CurrentSourcePageType == typeof(Startpage))
App.Current.Exit();
// Navigate back
if (mMainFrame.CanGoBack)
{
mMainFrame.GoBack();
}
}
這篇關于處理返回導航 Windows 10 (UWP)的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!