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

前端 未结 3 1519
有刺的猬
有刺的猬 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:03

    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 messages = new List();
    
    
        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());
            }
    
        }
    
    }
    

提交回复
热议问题