2015-11-06 63 views

回答

2

可以使用BackRequested事件來處理回退請求:

SystemNavigationManager.GetForCurrentView().BackRequested += OnBackRequested; 

if (App.MasterFrame.CanGoBack) 
{ 
    rootFrame.GoBack(); 
    e.Handled = true; 
} 
+0

這個'SystemNavigationManager'位於何處?我無法找到它。 'Windows.UI.Core'命名空間中的 – SandRock

+0

。 VS應該建議你。 – thang2410199

+0

好的。這可能是因爲我的目標是8.1。 – SandRock

2

點點解釋回答。 您可以使用命名空間

SystemNavigationManagerWindows.UI.Core對於單頁


如果你只是想處理單頁面導航。遵循以下步驟

步驟1。使用命名空間Windows.UI.Core

using Windows.UI.Core; 

第2步:註冊回請求事件當前視圖。最好的地方是InitializeComponent()之後的課程的主要構造函數。

public MainPage() 
{ 
    this.InitializeComponent(); 
    //register back request event for current view 
    SystemNavigationManager.GetForCurrentView().BackRequested += MainPage_BackRequested; 
} 

第3步:手柄BackRequested事件

private void Food_BackRequested(object sender, BackRequestedEventArgs e) 
{ 
    if (Frame.CanGoBack) 
    { 
     Frame.GoBack(); 
     e.Handled = true; 
    } 
} 

應用程序完全在一個地方進行單rootFrame


處理所有後退按鈕所有視圖最好的地方是App.xaml.cs

步驟1。使用命名空間Windows.UI.Core

using Windows.UI.Core; 

第2步:註冊回請求事件當前視圖。這個最好的地方是OnLaunched之前Window.Current.Activate

protected override void OnLaunched(LaunchActivatedEventArgs e) 
{ 
    ... 
    SystemNavigationManager.GetForCurrentView().BackRequested += OnBackRequested; 
    Window.Current.Activate(); 
} 

第3步:手柄BackRequested事件

private void OnBackRequested(object sender, BackRequestedEventArgs e) 
{ 
    Frame rootFrame = Window.Current.Content as Frame; 
    if (rootFrame.CanGoBack) 
    { 
     rootFrame.GoBack(); 
     e.Handled = true; 
    } 
} 

引用 - Handle back button pressed in UWP

希望這是有幫助的人!

0

上述代碼完全正確,但您必須在rootFrame變量中添加框架對象。以下給出:

private Frame _rootFrame; 
protected override void OnLaunched(LaunchActivatedEventArgs e) 
{ 
     Frame rootFrame = Window.Current.Content as Frame; 
     if (Window.Current.Content==null) 
     { 
      _rootFrame = new Frame(); 
     } 
} 

並將此_rootFrame傳遞給OnBackRequested方法。像:

private void OnBackRequested(object sender, BackRequestedEventArgs 
{ 
     if (_rootFrame.CanGoBack) 
     { 
      _rootFrame.GoBack(); 
      e.Handled = true; 
     } 
}