2

I don't think I need to paste code. This is C#.

Basically, I have a MessageDialog Created, Show it, and have UICommandInvokedHandler being called from a button.

Inside that Handler, I do other items that could cause another MessageDialog.ShowAsync to be called. However, this second call is giving an Unathorized Access Exception.

I've tried some things like causing an event trying to force the new MessageDialog to be created on the UI THread, but that did the same thing.

Any suggestions on how to get around this? Roughly, I am trying to give a Dialog to say "Are you sure? Yes/No" If yes, it does the guts of execution and can pop up other Dialogs to give random error information.

Ok Code:

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

Later.....

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

Calls this.....

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

1 回答 1

3

Well, the core of the problem is you're trying to bring up a MessageDialog while one is still in play.

There may be more elegant approaches, but you could use the return of ShowAsync to identify the selected command then explicitly call its handler, that way the first popup is dismissed before the second appears. A quick test on this end indicates that would work.

于 2013-04-10T06:50:33.733 回答