【问题标题】:What is the alternative to `alert` in metro apps?Metro 应用程序中“警报”的替代方法是什么?
【发布时间】:2012-11-19 02:21:54
【问题描述】:

我在 Windows 8 vs 2012 上创建了我的第一个应用程序,它运行良好。但是当我尝试像这样从 JavaScript 中说“helloworld”时:

alert("Hello World");

我收到一个错误:

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

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

如果alert 已过时,还有什么替代方案?

【问题讨论】:

    标签: javascript html microsoft-metro winjs


    【解决方案1】:

    请记住,alert 不是 JavaScript 函数,它是浏览器(主机)函数,因此,它在非浏览器环境中不可用。

    This link 告诉您执行以下操作

    • 将所有警报功能替换为触发事件window.external.notify("message")

    • 使用 webview 中的 scriptnotify 事件来获取该消息。

    • 显示 Metro 自己的对话框:MessageDialog

    【讨论】:

    • 这适用于托管 web 视图的 xaml 应用程序,但不适用于纯 JS 应用程序。
    【解决方案2】:

    你应该使用Windows.UI.Popups.MessageDialog:

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

    但是,您应该注意:

    • 没有像熟悉的警报那样阻止
    • 因为它没有阻止,您可以尝试向他们显示多个消息框;这是不允许的。

    我回答了另一个类似here 的问题。这是允许您调用警报并在飞行中发送多条消息的代码:

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

    【讨论】:

    • 不错的解决方案!不过需要注意的是 - 显示警报是异步操作是有原因的:所以你仍然可以做一些事情。因此,如果您想向用户显示警报,但仍要在后台进行一些处理,则绝对可以使用 ShowAsync,并自己处理“完成”。
    【解决方案3】:

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

    【讨论】:

      猜你喜欢
      • 2011-03-30
      • 1970-01-01
      • 1970-01-01
      • 2016-06-24
      • 1970-01-01
      • 2013-08-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多