What is the alternative to `alert` in metro apps?

前端 未结 3 1507
有刺的猬
有刺的猬 2021-01-14 20:55

I created my first app on Windows 8 vs 2012 and it runs and works fine. But when I try to say \"helloworld\" from JavaScript like this:

alert(\"Hello World\         


        
3条回答
  •  一向
    一向 (楼主)
    2021-01-14 21:05

    You should use Windows.UI.Popups.MessageDialog:

    (new Windows.UI.Popups.MessageDialog("Content", "Title")).showAsync().done();
    

    However, you should be aware that:

    • This is not blocking like the familiar alert
    • Because it's not blocking you may try to show them multiple messages boxes; this isn't allow.

    I answered another question like this here. Here's the code to allow you to call alert, and have multiple messages in flight:

    (function () {
        var alertsToShow = [];
        var dialogVisible = false;
    
        function showPendingAlerts() {
            if (dialogVisible || !alertsToShow.length) {
                return;
            }
    
            dialogVisible = true;
            (new Windows.UI.Popups.MessageDialog(alertsToShow.shift())).showAsync().done(function () {
                dialogVisible = false;
                showPendingAlerts();
            })
        }
        window.alert = function (message) {
            if (window.console && window.console.log) {
                window.console.log(message);
            }
    
            alertsToShow.push(message);
            showPendingAlerts();
        }
    })();
    

提交回复
热议问题