简体   繁体   中英

How can i close MessageDialog from code in HTML5 Windows Store app?

As in the title,

how can i close a MessageDialog from code in HTML5 Windows Store app?

my code so far:

var msg = new Windows.UI.Popups.MessageDialog("Please wait");
msg.commands.append(new Windows.UI.Popups.UICommand("OK",
    function (command) {
        ...
    }));
msg.showAsync();

and then i would like to close this popup FROM THE CODE, i have not found any methods in the specification like

msg.close();

is there a way?

thanks

The fact that your message is "Please wait" suggests to me that you might want to use a different tool for this job.

If what you're trying to do is inform the user that you're doing something in the background that they need to wait for, consider using a Progress control instead, as documented here:

http://msdn.microsoft.com/en-us/library/windows/apps/hh465487.aspx

If you use a Progress control, you can both include a text label with your desired text, and dismiss the progress control when you've finished whatever task it is you're asking the user to wait for.

And to answer your original question, I don't believe there's any API for dismissing a MessageDialog programmatically, as that would break the interaction pattern of this control, which is for the app to display a message, and then allow the user to dismiss it when they're ready to.

Hope that helps.

For more information on Windows Store app development, register for App Builder .

I think you want to use a flyout, similarly to this answer . The link solves a slightly different problem in that it closes the flyout after a timeout.

However, you should be able to define a flyout that you, as well as the user, can close. In both cases, you end up calling something like:

flyout.winControl.hide(); // Dismiss the flyout

Take a look at this...

(function(){
"use strict"; 
    var page = WinJS.UI.Pages.define("/html/cancelcommand.html", { 
        ready: function (element, options) { 
            document.getElementById("cancelCommand").addEventListener("click", cancelCommand_Click, false); 
        } 
    }); 

    // Click handler for the 'cancelCommand' button. 
    // Demonstrates setting the command to be invoked when the 'escape' key is pressed. 
    // Also demonstrates retrieval of the label of the chosen command and setting a callback to a function. 
    // A message will be displayed indicating which command was invoked. 
    // In this scenario, 'Try again' is selected as the default choice, and the 'escape' key will invoke the command named 'Close' 
    function cancelCommand_Click() { 
        // Create the message dialog and set its content 
        var msg = new Windows.UI.Popups.MessageDialog("No internet connection has been found."); 

        // Add commands and set their command handlers 
        msg.commands.append(new Windows.UI.Popups.UICommand("Try again", commandInvokedHandler)); 
        msg.commands.append(new Windows.UI.Popups.UICommand("Close", commandInvokedHandler)); 

        // Set the command that will be invoked by default 
        msg.defaultCommandIndex = 0; 

        // Set the command to be invoked when escape is pressed 
        msg.cancelCommandIndex = 1; 

        // Show the message dialog 
        msg.showAsync(); 
    } 

    function commandInvokedHandler(command) { 
        // Display message 
        WinJS.log && WinJS.log("The '" + command.label + "' command has been selected.", "sample", "status"); 
    } 
}());

http://code.msdn.microsoft.com/windowsapps/Message-dialog-sample-00c928f5/sourcecode?fileId=50972&pathId=1064922824

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM