MessageDialog ShowAsync抛出accessdenied异常

时间:2016-07-05 18:39:37

标签: windows-runtime messagedialog

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();
});

1 个答案:

答案 0 :(得分:0)

一旦我发现问题来自于在MessageDialog中打开MessageDialog,我就可以使用此处实现的解决方案:

MessageDialog ShowAsync throws accessdenied exception on second dialog