【发布时间】:2017-01-08 19:55:30
【问题描述】:
在我的 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 调试问题已解决,但在移动调试中,返回按钮再次不起作用。
【问题讨论】:
-
你可以试试
SystemNavigationManager.GetForCurrentView().BackRequested += OnBackRequested; SystemNavigationManager.GetForCurrentView().AppViewBackButtonVisibility = AppViewBackButtonVisibility.Visible;而不是`HardwareButtons.BackPressed' -
我的问题还是一样,它在移动和 PC 中再次通过异常这是我的 app.xaml.cs 文件的链接,如果你可以帮助我1drv.ms/u/s!Ahppl6XCmlXfj_VUrekI819VqZLG-Q
标签: c# win-universal-app uwp windows-10-mobile mobile-development