5

I am trying to close a MessageDialog in my WinRT App. I have noticed if I attempt to show two message dialogs at once, I get an UnauthorizedAccessException. To avoid this, I want to close the existing message dialog if it is open. I use this to show the dialog:

    MessageDialog md = new MessageDialog(" ");

    private void MessageBox(string s)
    {
        Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () =>
        {
            md.Content = s;
            //CLOSE HERE
            md.ShowAsync();
        }
        );
    }

How do I close it?

msbg
  • 4,852
  • 11
  • 44
  • 73
  • 1
    You should really make the MessageBox function async and "await" the RunAsync call inside it. That's architecturally cleaner and allows you to catch any errors that occur during the call to RunAsync. – Larry Osterman Oct 03 '12 at 00:32
  • Why would you show two message boxes? Maybe they are not want to you to do. What do you want to do? – Manuel Rauber Oct 03 '12 at 06:46
  • 1
    This method is a generic method I use to show message boxes, and it might be called twice. – msbg Oct 09 '12 at 17:02

1 Answers1

6

instead of trying to find a way to close it, try this declare a instance variable for AsyncCommand;

AsyncCommand command;

command = md.ShowAsync();

then in your commandhandler, before running your method check if command is null

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

// do stuff/ tryagain block

Syler
  • 1,151
  • 1
  • 13
  • 30
  • Is there a solution like this in WinJS rather than C#? – ariestav Aug 29 '13 at 16:08
  • take a look at the code snippet here. http://msdn.microsoft.com/library/windows/apps/BR208674?cs-save-lang=1&cs-lang=javascript#code-snippet-1 – Syler Sep 26 '13 at 07:54