在Windows 10 Universal应用程序中,我想在按下后退按钮时显示MessageDialog。
我的页面代码如下:
protected override void OnNavigatedTo(NavigationEventArgs e)
{
base.OnNavigatedTo(e);
SystemNavigationManager.GetForCurrentView().AppViewBackButtonVisibility = AppViewBackButtonVisibility.Visible;
SystemNavigationManager.GetForCurrentView().BackRequested += GamePage_BackRequested;
}
private async void GamePage_BackRequested(object sender, BackRequestedEventArgs e)
{
var dialog = new Windows.UI.Popups.MessageDialog("Are you sure ?");
dialog.Commands.Add(new Windows.UI.Popups.UICommand("Yes"));
dialog.Commands.Add(new Windows.UI.Popups.UICommand("No"));
var result = await dialog.ShowAsync();
}
当我在“本地机器”中启动应用程序时,对话框会很好地显示。但是当我将Windows转为“平板电脑模式”,或者当我在Windows Phone上试用它时,ShowAsync方法会崩溃应用程序(没有错误)。
为什么应用程序崩溃?
答案 0 :(得分:2)
问题似乎是" dialog.ShowAsync()"应该从UI线程调用方法。
这就是我解决它的方法:
private void GamePage_BackRequested(object sender, BackRequestedEventArgs e)
{
e.Handled = true;
Frame rootFrame = Window.Current.Content as Frame;
if (rootFrame.CanGoBack)
{
var d = dispatcher.RunAsync(CoreDispatcherPriority.Normal, () => ShowConfirmationDialog(rootFrame));
}
}
public async void ShowConfirmationDialog(Frame rootFrame)
{
var dialog = new Windows.UI.Popups.MessageDialog("Are you sure ?");
dialog.Commands.Add(new Windows.UI.Popups.UICommand("Yes") { Id = 0 });
dialog.Commands.Add(new Windows.UI.Popups.UICommand("No") { Id = 1 });
var result = await dialog.ShowAsync();
if (result != null && result.Label == "Yes")
{
rootFrame.GoBack();
}
}
protected override void OnNavigatedTo(NavigationEventArgs e)
{
base.OnNavigatedTo(e);
dispatcher = Windows.UI.Core.CoreWindow.GetForCurrentThread().Dispatcher;
SystemNavigationManager.GetForCurrentView().BackRequested += GamePage_BackRequested;
}
protected override void OnNavigatedFrom(NavigationEventArgs e)
{
base.OnNavigatedFrom(e);
SystemNavigationManager.GetForCurrentView().BackRequested -= GamePage_BackRequested;
}
答案 1 :(得分:1)
You should handle the backrequest ; e.handled = true;
private async void GamePage_BackRequested(object sender, BackRequestedEventArgs e)
{
e.handled = true;
var dialog = new Windows.UI.Popups.MessageDialog("Are you sure ?");
dialog.Commands.Add(new Windows.UI.Popups.UICommand("Yes"));
dialog.Commands.Add(new Windows.UI.Popups.UICommand("No"));
var result = await dialog.ShowAsync();
}
You should add onnavigatedfrom method to unregister the event, otherwise it will trigger twice !
protected override void OnNavigatedFrom(NavigationEventArgs e)
{
if (gb.DetectPlatform() == Platform.WindowsPhone)
HardwareButtons.BackPressed -= HardwareButtons_BackPressed;
elde
SystemNavigationManager.GetForCurrentView().BackRequested -= GamePage_BackRequested;
};
}
答案 2 :(得分:0)
没有办法实现你想要的 - 后面的键处理程序需要立即回答(处理与否),但对话框本质上是异步的。
当然,您可以选择在显示对话框之前将事件标记为Handled
,但如果用户显示"是"那时你无法走开。您当然可以终止该应用,但这不是一个坏主意(参见最后一段)
也就是说,您通常不需要此对话框,因为在Windows 10中,退出应用程序并不会终止它,它只是切换到之前的应用程序(或“开始”菜单)。用户可以通过任务切换器(或再次启动它)轻松返回到它。
答案 3 :(得分:0)
当我在MessageDialog.ShowAsync
事件处理程序中调用Application.UnhandledException
时,我的UWP应用程序崩溃了,这是我搜索答案时的最高结果。
我通过设置UnhandledExceptionEventArgs.Handled = true
之前 调用MessageDialog.ShowAsync
解决了该问题。
another SO question中对此进行了很好的记录,但是直到我找出解决方案后,我才找到答案,因为我没有意识到从Application.UnhandledException
调用该方法很重要。