locked
Unauthorized exception when using MessageDialog RRS feed

  • Question

  • I have a ViewModel which instantiates an event in a synchronous method.  The event signals to the UI that we need a "Yes" or "No" answer from the user before continuing.

    I am trying to display a MessageDialog and wait until the user provides an answer - Yes or No.  I am having a difficult time doing this.  I currently get an UnauthorizedAccess exception when trying to do this.

    Here's a look at the code in the UI:

    async Task<bool> Instance_SwitchConfirmation(string question)
    {
     MessageDialog md = new MessageDialog(question);
     md.Commands.Add(new UICommand("Yes", CommandInvokedHandler));
     md.Commands.Add(new UICommand("No", CommandInvokedHandler));
     md.ShowAsync();

     return this.canSwitch;
    }

    async void CommandInvokedHandler(IUICommand command)
    {
     this.canSwitch = command.Label == "Yes" ? true : false;
    }

    I have tried:

    var uiContext = TaskScheduler.FromCurrentSynchronizationContext();
                Task.Factory.StartNew(() => {
                    MessageDialog md = new MessageDialog(question);
                    md.Commands.Add(new UICommand("Yes", CommandInvokedHandler));
                    md.Commands.Add(new UICommand("No", CommandInvokedHandler));
                    md.ShowAsync();

                }, new System.Threading.CancellationToken(), TaskCreationOptions.PreferFairness, uiContext);

    but this fails with the same exception.
     
     
    If MessageDialog had a synchronous version of Show() I would be fine but the asynchrnous behvaior of MessageDialog coupled with my synchrounous routine, coupled with cross threading is confusing me.  I'm wondering if someone can outline what I need to do to wait for user input on a MessageDialog before continuing a synchronous method in my backend ViewModel.

    Thanks in advance for the help.

    Sunday, June 23, 2013 4:45 PM

Answers

  • async Task<bool> Instance_SwitchConfirmation(string question) { MessageDialog md = new MessageDialog(question); md.Commands.Add(new UICommand("Yes", CommandInvokedHandler)); md.Commands.Add(new UICommand("No", CommandInvokedHandler)); await md.ShowAsync(); return this.canSwitch; }

    You need to add an await on the show async.



    British Airways Inspiration App & rara music

    • Marked as answer by CHI MS DEV Tuesday, June 25, 2013 12:50 AM
    Sunday, June 23, 2013 10:47 PM

All replies

  • async Task<bool> Instance_SwitchConfirmation(string question) { MessageDialog md = new MessageDialog(question); md.Commands.Add(new UICommand("Yes", CommandInvokedHandler)); md.Commands.Add(new UICommand("No", CommandInvokedHandler)); await md.ShowAsync(); return this.canSwitch; }

    You need to add an await on the show async.



    British Airways Inspiration App & rara music

    • Marked as answer by CHI MS DEV Tuesday, June 25, 2013 12:50 AM
    Sunday, June 23, 2013 10:47 PM
  • Thanks for your response.  When I use the code suggested, the MessageDialog is not displayed and the UI thread locks up.  Why would that happen?  Please let me know, thanks.

    Monday, June 24, 2013 11:48 AM
  • are you sure the UI thread locks up? can you describe what you do. can show how you call this method: Instance_SwitchConfirmation

    Microsoft Certified Solutions Developer - Windows Store Apps Using C#

    Tuesday, June 25, 2013 12:03 AM
  • I figured out my issue - thanks all for the help.
    Tuesday, June 25, 2013 12:49 AM