admin管理员组

文章数量:1279057

I'm writing a Windows 8 application in C# and XAML that hosts a Web page in a WebView. The Web page makes calls to alert() and confirm(), but the WebView throws exceptions, saying, "'alert' is undefined," or "'confirm' is undefined." Can alert() and confirm() be enabled, or do I need to write code to emulate their normal function? And if I need to write them myself, how should I begin such an undertaking? Thank you.

I'm writing a Windows 8 application in C# and XAML that hosts a Web page in a WebView. The Web page makes calls to alert() and confirm(), but the WebView throws exceptions, saying, "'alert' is undefined," or "'confirm' is undefined." Can alert() and confirm() be enabled, or do I need to write code to emulate their normal function? And if I need to write them myself, how should I begin such an undertaking? Thank you.

Share Improve this question asked Aug 8, 2012 at 15:50 user1325179user1325179 2,1153 gold badges23 silver badges31 bronze badges
Add a ment  | 

3 Answers 3

Reset to default 5

Alert and confirm will not work from a WebView. You can use WebView.ScriptNotify to receive an event from your script on your page and use that event to show a dialog box using the MessageDialog class.

In your script where you want the alert ...

window.external.notify('foo');

And use the example in the WebView.ScriptNotify method in your C#.

You need to do some workaround. I did this and it worked for me.

       window.alert = function(__msg){window.external.notify(' + __msg + ');};

refer a similar question of mine for windows phone here

You will need to write code to enable them. Note that this is true in a HTML5 Win8 app as well as XAML.

For script that can access WinRT, you can use Windows.UI.Popup.MessageDialog:

(new Windows.UI.Popup.MessageDialog("Hello!", "Wele")).showAsync().done(dismissedHandler)

Note that these are async, and not block execution.

For acquiring information form the user -- e.g text input from the user, there is nothing standard, and you'll need to create a new set of mark up for that.

本文标签: C WebView Doesn39t Understand JavaScript Call of alert() (Windows 8 XAML Application)Stack Overflow