c# - WinRT C# MessageDialog.ShowAsync - 未经授权的访问异常 -

标签 c# xaml windows-runtime

我认为我不需要粘贴代码。这是 C#。

基本上,我创建了一个 MessageDialog,显示它,并从按钮调用 UICommandInvokedHandler。

在该处理程序中,我执行了其他可能导致调用另一个 MessageDialog.ShowAsync 的操作。但是,第二次调用给出了未经授权的访问异常。

我尝试过一些事情,比如引发一个事件,试图强制在 UI 线程上创建新的 MessageDialog,但效果是一样的。

关于如何解决这个问题有什么建议吗?粗略地说,我试图给出一个对话框来说“你确定吗?是/否”如果是,它会执行执行的任务,并且可以弹出其他对话框来提供随机错误信息。

好的代码:

    public static async void WriteMessageDialog(string message, string buttonText1, UICommandInvokedHandler handler1, string buttonText2, UICommandInvokedHandler handler2)
    {
        MessageDialog msgDlg = new MessageDialog(message);
        msgDlg.Commands.Add(new UICommand(buttonText1, handler1));
        msgDlg.Commands.Add(new UICommand(buttonText2, handler2));

        // Set the default button to be enabled and default on escape key pressed
        msgDlg.DefaultCommandIndex = 0;
        msgDlg.CancelCommandIndex = 0;

        // Show the window
        await msgDlg.ShowAsync();
    }

稍后......

// THey original Message Dialog
RTUtilities.WriteMessageDialog(
       _resourceLoader.GetString("DetelePersonConfirm"),         
       _resourceLoader.GetString("Delete"), 
       new UICommandInvokedHandler(this.CommandDeletePersonHandler), _resourceLoader.GetString("Cancel"), 
       new UICommandInvokedHandler(this.CommandCancelHandler));

调用此......

    private async void CommandDeletePersonHandler(IUICommand command)
    {
        MessageDialog msgDlg = new MessageDialog(_resourceLoader.GetString("DeleteIndividualError"));
        await msgDlg.ShowAsync();
    }

最佳答案

嗯,问题的核心是您试图在 MessageDialog 仍在运行时调出 MessageDialog。

可能有更优雅的方法,但您可以使用 ShowAsync 的返回识别所选命令,然后显式调用其处理程序,这样第一个弹出窗口将在第二个弹出窗口出现之前关闭。对此进行的快速测试表明这是可行的。

关于c# - WinRT C# MessageDialog.ShowAsync - 未经授权的访问异常 -,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15915269/

相关文章:

c# - Microsoft Office 应用程序的主要互操作程序集

performance - Windows Phone 8 应用程序分析中的纹理内存占主导地位是什么意思?

c# - 如何打开以编程方式添加到我的 Windows 应用商店应用程序项目中的文件?

c# - 具有基于类型的数据模板选择和绑定(bind)的 Contentpresenter

wpf - 仅带有垂直滚动条的 TextBlock

windows - 从特定文件夹中获取特定文件

c# - 如何通过 ODBC C# 绑定(bind)参数?

c# - 使带有内部修饰符的测试类对单元测试框架可见

c# - Ninject 的 InSingletonScope() 创建多个实例

.net - 为什么我不能在 2.0.0.0 播放器框架中重用来自 1.8.2.2 播放器框架的自定义 XAML 样式 PlayPauseButton