MessageDialog ShowAsync throws accessdenied exception on second dialog
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 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.
I am late to the party, but here's a way where you can always await the result of the dialog box, as well as not need to worry about calling too many in a row:
First define a static variable and method in your application:
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; }
Now, you can pass in any dialog box and always await execution. This is why this returns a bool rather than void. You won't have to worry about collisions between multiples. Why not make this method accept a string? Because of title, and Yes/No command handlers that you may assign into the specific dialog box you are using.
Invoke such as:
await App.ShowDialog(new MessageDialog("FOO!"));
or
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);
There is an answer for this on the MSDN forums that might help you here.
I'm having a similar problem but my showAsync calls are in separate functions on separate threads so I can't drop a done() in there I don't think...