处理返回导航 Windows 10 (UWP)

Handling Back Navigation Windows 10 (UWP)(处理返回导航 Windows 10 (UWP))
本文介绍了处理返回导航 Windows 10 (UWP)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

在我的 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)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

相关文档推荐

Custom Error Queue Name when using EasyNetQ for RabbitMQ?(使用 EasyNetQ for RabbitMQ 时自定义错误队列名称?)
How to generate password_hash for RabbitMQ Management HTTP API(如何为 RabbitMQ 管理 HTTP API 生成密码哈希)
Rabbitmq Ack or Nack, leaving messages on the queue(Rabbitmq Ack 或 Nack,将消息留在队列中)
Setup RabbitMQ consumer in ASP.NET Core application(在 ASP.NET Core 应用程序中设置 RabbitMQ 消费者)
Specify Publish timeouts in mass transit(指定公共交通中的发布超时)
RabbitMQ asynchronous support(RabbitMQ 异步支持)