处理后退导航Windows 10(UWP)

Sha*_*iar 15 c# navigation windows-10 windows-10-mobile

在我的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();
}
Run Code Online (Sandbox Code Playgroud)

但是在做完HardwareButtons_BackPressed活动后的电话中关闭了应用程序.

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

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


[更新]

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

SystemNavigationManager currentView = SystemNavigationManager.GetForCurrentView();
Run Code Online (Sandbox Code Playgroud)

Vin*_*ary 21

Windows 10(UWP)在命名空间中包含SystemNavigationManager,Windows.UI.Core仅用于导航.

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

对于单页


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

第1步.使用命名空间Windows.UI.Core

using Windows.UI.Core;
Run Code Online (Sandbox Code Playgroud)

步骤2.为当前视图注册返回请求事件.最好的地方是后来的主要构造函数InitializeComponent().

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

第3步.处理BackRequested事件

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

适用于单个地方的完整应用 rootFrame


处理所有视图的所有后退按钮的最佳位置是 App.xaml.cs

第1步.使用命名空间Windows.UI.Core

using Windows.UI.Core;
Run Code Online (Sandbox Code Playgroud)

步骤2.为当前视图注册返回请求事件.最好的地方OnLaunched就在此之前Window.Current.Activate

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

第3步.处理BackRequested事件

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

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

希望这对某人有帮助!


Tob*_*fer 7

您需要通过将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();
        }
  }
Run Code Online (Sandbox Code Playgroud)