最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - What is the alternative to `alert` in metro apps? - Stack Overflow

programmeradmin1浏览0评论

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");

I get an error:

Unhandled exception at line 21,
 column 13 in ms-appx://1af489cf-bac6-419b-8542-fdc18bdd2747/default.html

0x800a1391 - JavaScript runtime error: 'alert' is undefined

What is the alternative if alert is obsolete?

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");

I get an error:

Unhandled exception at line 21,
 column 13 in ms-appx://1af489cf-bac6-419b-8542-fdc18bdd2747/default.html

0x800a1391 - JavaScript runtime error: 'alert' is undefined

What is the alternative if alert is obsolete?

Share Improve this question edited Nov 30, 2012 at 22:40 KatieK 13.9k19 gold badges78 silver badges90 bronze badges asked Nov 30, 2012 at 20:09 SmartboySmartboy 9846 gold badges24 silver badges37 bronze badges
Add a ment  | 

3 Answers 3

Reset to default 12

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();
    }
})();

Remember that alert is not a JavaScript function, it's a browser (host) function, therefore, it's not available in non browser environments.

This link tells you to do the following

  • Replace all alert functions with firing an event window.external.notify("message");

  • Use scriptnotify event in webview to get that message.

  • Show metro own dialog: MessageDialog

javascript:

 (function () {

  window.alert = function (message) {
      window.external.notify( message);
  }
//do some test
alert("a");
alert("b");
alert("c");
window.setInterval(function () {
    alert("e");
    alert("f");
}, 5000);
window.setInterval(function () { 
    alert("d");
    alert("2");
}, 10000);
 })();

C#:

  //register ScriptNotify event
  webView2.ScriptNotify += webView2_ScriptNotify;

async void webView2_ScriptNotify(object sender, NotifyEventArgs e)
    {
        MSG.Alert(e.Value);
    }


public class MSG
{
    static List<string> messages = new List<string>();


    public static void Alert(string message)
    {

        messages.Add(message);
        if (messages.Count == 1)
        {
              Show(messages.First());
        }

    }

    private static async Task Show(string message)
    {
        MessageDialog md = new MessageDialog(message, "Title");

        md.Commands.Add(
           new UICommand("OK", new UICommandInvokedHandler((cmd) =>
           {
               messages.RemoveAt(0);

           })));



        await md.ShowAsync();

        while (messages.Count > 0)
        {

            await Show(messages.First());
        }

    }

}
发布评论

评论列表(0)

  1. 暂无评论