2015-11-06 69 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; 
     } 
}