1

我正在尝试使用 WP7 中的 Web 浏览器控件在网页中使用 javascript 显示警报框。没有弹出警报。代码有什么问题还是 WP7 根本不支持它?

<phone:WebBrowser Name="browser" IsScriptEnabled="True" ScriptNotify="browser_ScriptNotify" Source="Default.html"/>

Default.html 内部

<html><br>
<head><br>
</head><br>
<body onload="onLoad()"><br>
    <script type="text/javascript"><br>
        function onLoad() {<br>
            alert("hello");<br>
        }<br>
    </script><br>
</body><br>
</html>
4

2 回答 2

2

您是否能够加载 default.html 资源?先看看http://blogs.msdn.com/b/dohollan/archive/2010/08/25/adventures-with-the-windows-phone-7-webbrowser-control.aspx

更新后包含如何实现预期效果的示例代码:

的HTML:

<html xmlns="http://www.w3.org/1999/xhtml" >
<head>
    <title></title>
    <script type="text/javascript" >
        function ShowNameAlert(name) {
            window.external.notify("Hello " + name);
        }
     </script>
 </head>
<body onload="ShowNameAlert('Jojo');">
Bla bla
</body>
</html>

C# 代码隐藏:

private void SomeBrowser_ScriptNotify(object sender, NotifyEventArgs e)
{
    MessageBox.Show(e.Value);
}

XAML:

<phone:WebBrowser x:Name="SomeBrowser" ScriptNotify="SomeBrowser_ScriptNotify" IsScriptEnabled="True" Source="test.html"/>
于 2012-08-07T16:41:21.160 回答
1

这就是我解决它的方法,我将 javascript 注入到我导航到的网页上,并覆盖了警报和确认框

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

然后在脚本通知函数中使用 MessageBox 显示该消息。我希望它也对其他人有所帮助。以前的答案是一种解决方法,这是我认为是解决我的问题的正确方法

于 2012-08-24T06:22:51.490 回答