在第二个对话框中抛出

在第二个对话框中抛出

本文介绍了MessageDialog ShowAsync 在第二个对话框中抛出 accessdenied 异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在 Windows 8 中实现重试/取消对话框.该对话框第一次显示正常,但在单击重试并再次失败时,我在调用 ShowAsync 时遇到访问被拒绝异常.我不知道为什么,但奇怪的是有时代码运行良好,当我放置断点时我没有收到异常.这里真的很无知

I am trying to implement try again/cancel dialog box in windows 8. The dialog box shows fine the first time, but on clicking try again and failing again, I get a access denied exception on calling ShowAsync.I don't know why, but its strange sometimes the code works fine and I don't get the exception when I put breakpoints. really clueless here

这是代码.

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

推荐答案

好的,我找到了一个快速的解决方案,

Okay I found a quick solution,

定义一个 IAsyncOperation 类变量

define a IAsyncOperation class varialble

IAsyncOperation<IUICommand> asyncCommand = null;

并将其设置为 MessageDialog 的 ShowAsync 方法

and set it to the ShowAsync method of MessageDialog

asyncCommand = msg.ShowAsync();

在重试/重试的命令处理程序中检查 asyncCommand 是否不为 null 并在必要时取消最后一个操作

In the command handler for retry/try againcheck if asyncCommand is not null and cancel the last operation if necessary

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

如果有更好的方法,请告诉我.

Please let me if there is a better approach to this.

这篇关于MessageDialog ShowAsync 在第二个对话框中抛出 accessdenied 异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-01 19:04