locked
MessageDialog.showAsync: why the different syntax?

    Question

  • Hello guys.

    quick question: while playing with the MessageDialog sample, I've noticed the following code is used for showing the dialog:

    //Show the message dialog
    msg.showAsync().operation.start();   

    What is going on here? Why the cchange in pattern? I expected to write it like this:

    //Show the message dialog
    msg.showAsync().then( function(selectedCmd){ } );

    btw, the js docs also seem to suggest this is the way to go:

    http://msdn.microsoft.com/en-us/library/windows/apps/windows.ui.popups.messagedialog.showasync.aspx

    comments? thanks.


    Luis Abreu

    Wednesday, February 8, 2012 10:43 PM

Answers

  • Hi Luis--what you're seeing is raw async code vs. the simplified code that uses promises. For a single async operation, the two are about the same complexity, but promises greatly simplify matters when there are nested async calls. We definitely recommend using the promises syntax unless there is a real need to do otherwise.

    There was a time--a year ago or so, when promises weren't yet available, and all async code used the raw pattern. The sample just has a leftover from that era. You can get away with the .operation.start() syntax in this scenario because the MessageDialog isn't returning any results that would need to be picked up by the caller. Otherwise the raw pattern is:

    var operation = obj.asyncCall();

    operation.completed = function (results) {

        //use results

    }

    operation.start();

    With promises, this just becomes:

    obj.ascynCall().then (function (results) {

        //use results

    });

    Thursday, February 9, 2012 12:44 AM

All replies

  • Hi Luis--what you're seeing is raw async code vs. the simplified code that uses promises. For a single async operation, the two are about the same complexity, but promises greatly simplify matters when there are nested async calls. We definitely recommend using the promises syntax unless there is a real need to do otherwise.

    There was a time--a year ago or so, when promises weren't yet available, and all async code used the raw pattern. The sample just has a leftover from that era. You can get away with the .operation.start() syntax in this scenario because the MessageDialog isn't returning any results that would need to be picked up by the caller. Otherwise the raw pattern is:

    var operation = obj.asyncCall();

    operation.completed = function (results) {

        //use results

    }

    operation.start();

    With promises, this just becomes:

    obj.ascynCall().then (function (results) {

        //use results

    });

    Thursday, February 9, 2012 12:44 AM
  • Hello Kraig.

    YEs, I thought that was it (and I've also teste the callback approach you mention). Should I assume that the beta release will have that code migrated to use promises?

    thanks.


    Luis Abreu

    Thursday, February 9, 2012 10:43 AM
  • Did this change again? The code below dies for us now on the showAsync(). Get an "Access is denied"...

    var messagedialogpopup = Windows.UI.Popups.MessageDialog("There was an error with the web service. It is as follows : " + request.responseText);
    messagedialogpopup.showAsync();
    Monday, June 4, 2012 11:05 PM
  • Harlequin,

    I was getting the same error and it took me a little while to track it down, but for me, I was getting that error when trying to call .ShowAsync() when a dialog was currently open. To recreate the error, just do the following:

    var msg1 = new Windows.UI.Popups.MessageDialog("Message 1");
    var msg2 = new Windows.UI.Popups.MessageDialog("Message 2");
    
    msg1.ShowAsync();
    msg2.ShowAsync();


    Notice how I'm not using await, that's because in this example, I want to call msg2.ShowAsync() while the current dialog is open, not after it has been dismissed.

    Not sure if that's why you're getting the error message, but here's at least one way to recreate the Access Denied exception from a MessageDialog.

    Wednesday, July 11, 2012 5:14 PM