【发布时间】:2016-07-07 21:35:32
【问题描述】:
MessageDialog 的 ShowAsync() 方法偶尔会失败。至于它是否有效,这几乎是一个硬币翻转:
private async Task CloseApp()
{
MessageDialog restartMessage = new MessageDialog("Changes have made a restart necessary.", "App must Restart")
restartMessage.Commands.Add(new UICommand("Close Application", (command) => { Application.Current.Exit(); }));
await restartMessage.ShowAsync(); // Code breaks here
Application.Current.Exit();
}
我发现另一个用户使用almost identical problem,但该页面上的每个解决方案都无法防止我的错误发生。他们的解决方案如下所示:
private async Task CloseApp()
{
IAsyncOperation<IUICommand> asyncCommand = null;
MessageDialog restartMessage = new MessageDialog("Changes have made a restart necessary.", "App must Restart")
restartMessage.Commands.Add(new UICommand("Close Application", (command) => { Application.Current.Exit(); }));
restartMessage.DefaultCommandIndex = 0;
asyncCommand = restartMessage.ShowAsync(); // Code *still* breaks here
Application.Current.Exit();
}
更新:
问题可能来自尝试在另一个 MessageDialog 调用的方法中对 MessageDialog 运行 ShowAsync()。您不能同时显示两个 MessageDialog,因此会引发错误。
我使用 Dispatchers 的解决方案......实际上仍然无法正常工作,但还是有一点:
MessageDialog restartMessage = new MessageDialog("Changes have made a restart necessary.", "App must Restart");
restartMessage.Commands.Add(new UICommand("Close Application", (command) => { Application.Current.Exit(); }));
CoreDispatcher cD = Window.Current.CoreWindow.Dispatcher;
await cD.RunAsync(CoreDispatcherPriority.Normal, async () =>
{
await restartMessage.ShowAsync();
});
【问题讨论】:
-
为什么在关闭第一个消息对话框后不尝试显示第二个消息对话框?我想您需要第一个的结果才能知道是否必须显示第二个。
-
我认为 CoreDispather 会导致在第一条消息完成后打开第二条消息......但显然它没有。作为记录,第一个消息对话框唯一调用我的 SaveDate() 方法。我正在使用的这段代码在 SaveData() 方法内部调用,当出现问题并且我需要关闭应用程序时。如果有办法在放置我的restartMessage之前强制关闭任何打开的MessageDialogs,我可以完成这项工作,但我不知道有任何方法可以做到这一点。
标签: windows-runtime messagedialog