在Windows 10 PC上运行时[UWP] [C#]硬件后退按钮错误? [英] [UWP][C#] Hardware back button error when run on Windows 10 PC?

查看:70
本文介绍了在Windows 10 PC上运行时[UWP] [C#]硬件后退按钮错误?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一些代码来处理硬件后退按钮,它在手机上正常工作。但它在PC上崩溃?


App.XAML.cs

 private async void HardwareButtons_BackPressed(object sender,Windows.Phone。 UI.Input.BackPressedEventArgs e)
{
Frame rootFrame = Window.Current.Content as Frame;
e.Handled = true;
if(rootFrame.CanGoBack)
{
rootFrame.GoBack();
}
else
{
var msg = new MessageDialog("你确定要退出吗?");
var okBtn = new UICommand(" Yes");
var cancelBtn = new UICommand(" No");
msg.Commands.Add(okBtn);
msg.Commands.Add(cancelBtn);
IUICommand result = await msg.ShowAsync();

if(result!= null&& result.Label ==" Yes")
{
Application.Current.Exit();
}
}
}

我有参考扩展名:Windows Mobile Extension,Windows桌面扩展。



解决方案

您好,


使用
SystemNavigationManager
并订阅其BackRequested事件,这对所有UWP设备都是可访问的,您无需添加一些延伸。  


I have some code to handle Hardware back button, it's working fine on Phone. But it's crash on PC?

App.XAML.cs

private async void HardwareButtons_BackPressed(object sender, Windows.Phone.UI.Input.BackPressedEventArgs e)
        {
            Frame rootFrame = Window.Current.Content as Frame;
            e.Handled = true;
            if (rootFrame.CanGoBack)
            {
                rootFrame.GoBack();
            }
            else
            {
                var msg = new MessageDialog("Are you sure to exit?");
                var okBtn = new UICommand("Yes");
                var cancelBtn = new UICommand("No");
                msg.Commands.Add(okBtn);
                msg.Commands.Add(cancelBtn);
                IUICommand result = await msg.ShowAsync();

                if (result != null && result.Label == "Yes")
                {
                    Application.Current.Exit();
                }
            }
        }

I have reference extension : Windows Mobile Extension, Windows Desktop Extension.

解决方案

Hello,

Use SystemNavigationManager and subscribe on its BackRequested event, This is accessible for all UWP devices and you don't have to add some extension.  


这篇关于在Windows 10 PC上运行时[UWP] [C#]硬件后退按钮错误?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆