windowswindows-8windows-runtimewinrt-xamlmessagedialog

MessageDialog ShowAsync throws accessdenied exception on second dialog


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

here is the code.

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

Solution

  • Okay I found a quick solution,

    define a IAsyncOperation class varialble

    IAsyncOperation<IUICommand> asyncCommand = null;
    

    and set it to the ShowAsync method of MessageDialog

    asyncCommand = msg.ShowAsync();
    

    In the command handler for retry/try again check 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.