-->

MessageDialog ShowAsync抛出的第二个对话框存取遭拒例外(MessageDial

2019-06-18 10:52发布

我想实现重试/取消Windows 8的对话框该对话框显示精细的第一次,但在点击再试一次又一次失败,我得到一个拒绝访问的调用ShowAsync例外。 我不知道为什么,但它有时奇怪的代码工作正常,当我把断点我不明白的例外。 真的在这里独领风骚

这里是代码。

    async void DismissedEventHandler(SplashScreen sender, object e)
    {
        dismissed = true;
        loadFeeds();
    }
    private async void loadFeeds()
    {
        await Dispatcher.RunAsync(CoreDispatcherPriority.Normal, async () =>
        {
            try
            {
                RSSDataSource rssDataSource = (RSSDataSource)App.Current.Resources["RSSDataSource"];
                if (rssDataSource != null)
                {
                    await rssDataSource.DownloadFeeds();
                    await rssDataSource.GetFeedsAsync();
                }

                AdDataSource ads = (AdDataSource)App.Current.Resources["AdDataSource"];

                if (ads != null)
                {
                    await ads.DownloadAds();
                }
                rootFrame.Navigate(typeof(HomePageView));

                Window.Current.Content = rootFrame;
            }
            catch
            {
                ShowError();
            }

        });
    }
    async void ShowError()
    {
        // There was likely a problem initializing
        MessageDialog msg = new MessageDialog(CONNECTION_ERROR_MESSAGE, CONNECTION_ERROR_TITLE);

        // Add buttons and set their command handlers
        msg.Commands.Add(new UICommand(COMMAND_LABEL_RETRY, new UICommandInvokedHandler(this.CommandInvokedHandler)));
        msg.Commands.Add(new UICommand(COMMAND_LABEL_CLOSE, new UICommandInvokedHandler(this.CommandInvokedHandler)));
        // Set the command to be invoked when a user presses 'ESC'
        msg.CancelCommandIndex = 0;

        await msg.ShowAsync();
    }

    /// <summary>
    /// Callback function for the invocation of the dialog commands
    /// </summary>
    /// <param name="command">The command that was invoked</param>
    private void CommandInvokedHandler(IUICommand command)
    {
        string buttonLabel = command.Label;
        if (buttonLabel.Equals(COMMAND_LABEL_RETRY))
        {
            loadFeeds();
        }
        else
        {
            // Close app
            Application.Current.Exit();
        }
    }

Answer 1:

好吧,我发现了一个快速的解决方案,

定义一个IAsyncOperation类varialble

IAsyncOperation<IUICommand> asyncCommand = null;

并将其设置为MessageDialog的ShowAsync方法

asyncCommand = msg.ShowAsync();

在重试命令处理程序/再试一次检查,如果asyncCommand不为空,如果需要取消上次操作

if(asyncCommand != null)
{
   asyncCommand.Cancel();
}

请让我,如果有这种更好的方法。



Answer 2:

我迟到了,但这里有一个方法,你可以随时听候对话框的结果,以及不需要担心调用连续太多:

首先定义应用程序中的静态变量和方法:

 private static IAsyncOperation<IUICommand> messageDialogCommand = null;
 public async static Task<bool> ShowDialog(MessageDialog dlg) {

    // Close the previous one out
    if (messageDialogCommand != null) {
       messageDialogCommand.Cancel();
       messageDialogCommand = null;
    }

    messageDialogCommand = dlg.ShowAsync();
    await messageDialogCommand;
    return true;
 }

现在,你可以通过任何对话框并随时听候执行。 这就是为什么这个返回一个布尔值,而不是无效的。 你会不会担心倍数之间的碰撞。 为什么不把这个方法接受一个字符串? 因为称号,并是/你可以分配到你所使用的特定对话框中没有命令处理程序。

调用如:

await App.ShowDialog(new MessageDialog("FOO!"));

要么

var dlg = new MessageDialog("FOO?", "BAR?");
dlg.Commands.Add(new UICommand("Yes", new UICommandInvokedHandler(YesHandler)));
dlg.Commands.Add(new UICommand("No", new UICommandInvokedHandler(NoHandler)));
await App.ShowDialog(dlg);


Answer 3:

有在MSDN论坛上,可以帮助你在这里为这个答案。

http://social.msdn.microsoft.com/Forums/en-US/winappswithhtml5/thread/c2f5ed68-aac7-42d3-bd59-dbf2673dd89b

我有一个类似的问题,但我的showAsync通话是在不同的功能在单独的线程,所以我不能降一()完成在那里我不认为...



Answer 4:

我正面临同样的问题前几天,我解决它等待ShowAsync,然后进行递归调用再次打开MessageDialog。

public async void ShowDlg(){
    Action cmdAction = null;
    var msgDlg = new MessageDialog("Content.", "Title");
    msgDlg.Commands.Add(new UICommand("Retry", (x) => {
    cmdAction = () => ShowDlg();
    }));
    msgDlg.Commands.Add(new UICommand("Cancel", (x) => {
    cmdAction = () => <Action associated with the cancel button>;
    }));
    msgDlg.DefaultCommandIndex = 0;
    msgDlg.CancelCommandIndex = 1;

    await msgDlg.ShowAsync();
    cmdAction.Invoke();
}

希望这有助于!



Answer 5:

另一种解决方案:

private bool _messageShowing = false;

// ...

if (!_messageShowing)
{
    _messageShowing = true;
    var messageDialog = new MessageDialog("Example");

    // ... "messageDialog" initialization

    Task<IUICommand> showMessageTask =  messageDialog.ShowAsync().AsTask();
    await showMessageTask.ContinueWith((showAsyncResult) =>
        {
            _messageShowing = false;
        });
}


文章来源: MessageDialog ShowAsync throws accessdenied exception on second dialog