处理后退导航Windows 10(UWP)

在我的Xaml页面中,我有一个Frame。

我正在尝试使用backButton事件来在框架内导航。

所以我试着用这段代码

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事件后的手机中它会关闭应用程序。

它似乎在MainPage上运行一些默认的后退按钮行为…

我该如何解决? 在Windows10中,他们是否添加新事件来处理后退导航?


[更新]

现在我发现在Windows 10中使用SystemNavigationManager而不是Input.HardwareButtons.BackPressed

 SystemNavigationManager currentView = SystemNavigationManager.GetForCurrentView(); 

您需要通过将BackPressedEventArgs的Handled属性设置为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)在Windows.UI.Core命名空间中包含SystemNavigationManager ,仅用于导航。

因为SystemNavigationManagerWindows Universal Platform一部分,所以它在Windows 10上运行的所有设备系列都支持,包括Mobile和PC。

对于单页


如果您只想处理单页导航。 请执行以下步骤

第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.为当前视图注册返回请求事件。 最好的地方是在Window.Current.Activate之前OnLaunched

 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; } } 

参考 – 在UWP中按下后退按钮

希望这对某人有帮助!

跟着这些步骤:

  • 在您的页面中添加两个全局变量,如下所示。

     private NavigationHelper navigationHelper; private RelayCommand _GoBackCommand; 
  • 然后在特定页面的构造函数中添加以下代码。

      // below code is to override the back navigation // hardware back button press event from navigationHelper _GoBackCommand = new RelayCommand ( () => this.CheckGoBack(), () => this.CanCheckGoBack() ); navigationHelper.GoBackCommand = _GoBackCommand; // --------- 
  • 然后添加我们刚刚在构造函数中声明的那些方法。

     private bool CanCheckGoBack() { // this should be always true to make sure the app handles back buton manually. return true; } private void CheckGoBack() { // this will be execute when back button will be pressed } 

PS。 – 为此,您可能需要在添加新页面时使用BasicPage而不是BlankPage

希望这会有所帮助..!

试试这个。它适用于帧背导航。

  protected override void OnNavigatedTo(NavigationEventArgs e) { HardwareButtons.BackPressed += HardwareButtons_BackPressed; } void HardwareButtons_BackPressed(object sender, BackPressedEventArgs e) { Frame rootFrame = Window.Current.Content as Frame; if (Frame.CanGoBack) { e.Handled = true; Frame.GoBack(); } } } 

我想这是因为您在页面中添加了HardwareButtons_BackPressed,而不是在app.xaml.cs.中。

在app.xaml.cs中:

 public App() { this.InitializeComponent(); this.Suspending += this.OnSuspending; HardwareButtons.BackPressed += HardwareButtons_BackPressed; } void HardwareButtons_BackPressed(object sender, BackPressedEventArgs e) { Frame rootFrame = Window.Current.Content as Frame; if (rootFrame != null && rootFrame.CanGoBack) { e.Handled = true; rootFrame.GoBack(); } } 

现在,手机的后退按钮可以在任何页面上使用。

然后,如果您想在任何页面中添加特定按钮:

在特定页面(或每个页面,如果你想):

 public void btn_return_Tapped(object sender, TappedRoutedEventArgs e) { Frame rootFrame = Window.Current.Content as Frame; if (rootFrame != null && rootFrame.CanGoBack) { e.Handled = true; rootFrame.GoBack(); } } 

资料来源: http : //windowsapptutorials.com/tips/general-tips/handling-the-back-button-in-a-windows-phone-8-1-app/