在我的 UWP 应用程序中,当我单击移动后退按钮应用程序关闭时,请将此代码添加到 app.xaml.cs
private async void HardwareButtons_BackPressed(object sender, BackPressedEventArgs e)
{
e.Handled = true;
Frame rootFrame = Window.Current.Content as Frame;
if (rootFrame.CanGoBack && rootFrame != null)
{
rootFrame.GoBack();
}
else
{
var msg = new MessageDialog("Confirm Close! \nOr Press Refresh Button to Go Back");
var okBtn = new UICommand("OK");
var cancelBtn = new UICommand("Cancel");
msg.Commands.Add(okBtn);
msg.Commands.Add(cancelBtn);
IUICommand result = await msg.ShowAsync();
if (result != null && result.Label == "OK")
{
Application.Current.Exit();
}
}
}
和
public App()
{
this.InitializeComponent();
this.Suspending += OnSuspending;
/* Because of this line my app work on mobile great but when
when i debug on pc it through exception "show in image" */
HardwareButtons.BackPressed += HardwareButtons_BackPressed;
}
当我在手机上调试应用程序时完成所有这些代码后,应用程序成功运行 - 移动调试:
但是当使用相同的代码在 pc 中调试时,它会显示此错误 - PC 调试:
当我删除HardwareButtons.BackPressed += HardwareButtons_BackPressed;
然后解决了 pc 调试问题但在移动调试中返回按钮再次不起作用。