如果用户想要在我的AppViewBackButton
App.xaml.cs
)时想要离开页面,我会要求确认
protected override void OnLaunched(LaunchActivatedEventArgs e)
{
...
SystemNavigationManager.GetForCurrentView().BackRequested += OnBackRequested;
...
}
Private async void OnBackRequested(object sender, BackRequestedEventArgs e)
{
Frame rootFrame = Window.Current.Content as Frame;
if (rootFrame.Content.GetType() == typeof(GamePage))
{
var op = await ShowAsync("Do you want to exit?", "Exit?");
if (op == MessageBoxResult.OK)
{
rootFrame.GoBack();
e.Handled = true;
}
}
else
{
if (rootFrame.CanGoBack)
{
e.Handled = true;
rootFrame.GoBack();
}
}
}
public async Task<MessageBoxResult> ShowAsync(string messageBoxText, string caption)
{
MessageBoxResult result = MessageBoxResult.None;
MessageDialog md = new MessageDialog(messageBoxText, caption);
md.Commands.Add(new UICommand("OK",
new UICommandInvokedHandler((cmd) => result = MessageBoxResult.OK)));
md.Commands.Add(new UICommand("Cancel",
new UICommandInvokedHandler((cmd) => result = MessageBoxResult.Cancel)));
md.CancelCommandIndex = (uint)md.Commands.Count - 1;
var op = await md.ShowAsync();
return result;
}
问题是var op = await md.ShowAsync();
没有等待结果。该应用程序短暂显示对话框,无需等待任何输入,只需关闭应用程序。
我目前没有测试我的Windows 10移动设备,因此我正在使用模拟器进行测试。模拟器或代码有问题吗?
此代码在我的桌面上运行良好。