在 WebBrowser 控件中,window.external 不可用
我想在 WebBrowser 控件中调用 C# 方法。下面是我的代码:
在 XAML 中,
<phone:WebBrowser Margin="0,0,0,0" Name="WebBrowserForDetails" VerticalAlignment="Top" Height="300" ScriptNotify="WebBrowserForDetails_ScriptNotify" IsScriptEnabled="True" />
在 C# 中,
protected override void OnNavigatedTo(NavigationEventArgs e)
{
string html = string.Format("<html><head><title></title><script type=\"text/javascript\">{0}</script></head><body><button onclick=\"call();\">Push</button>", "function call(){ window.external.notify(123) ;}");
WebBrowserForDetails.NavigateToString(html);
}
private void WebBrowserForDetails_ScriptNotify(Object sender, NotifyEventArgs e)
{
Debug.WriteLine(e.Value);
}
预计在调试窗口中看到 123
。
当
编辑
参考链接:MSDN:window.external.notify
,设置 WP7 Webbrowser 控件的任何方法高度动态并锁定滚动? 和 显示 HTML Windows Phone 7 中的内容
I'd like to call C# methods within a WebBrowser Control. Below is my code:
In XAML,
<phone:WebBrowser Margin="0,0,0,0" Name="WebBrowserForDetails" VerticalAlignment="Top" Height="300" ScriptNotify="WebBrowserForDetails_ScriptNotify" IsScriptEnabled="True" />
In C#,
protected override void OnNavigatedTo(NavigationEventArgs e)
{
string html = string.Format("<html><head><title></title><script type=\"text/javascript\">{0}</script></head><body><button onclick=\"call();\">Push</button>", "function call(){ window.external.notify(123) ;}");
WebBrowserForDetails.NavigateToString(html);
}
private void WebBrowserForDetails_ScriptNotify(Object sender, NotifyEventArgs e)
{
Debug.WriteLine(e.Value);
}
Expected to see 123
in the debug window.
When the <button>Push</button>
is pushed, window.external.notify
is never called. In fact, window.external
is not available. I would like to call the window.external.notify
function from the WebBrowser control to call the WebBrowserForDetails_ScriptNotify
method. What should I do?
Edit
Reference links: MSDN: window.external.notify
, Any way to set the WP7 Webbrowser control height Dynamically and lock scrolling? and Displaying HTML Content in Windows Phone 7
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我怀疑您的问题是由于您的 (body & html) 元素未关闭而未提供有效的 HTML5 文档。
I suspect that your issue is due to not providing a valid HTML5 document as your (body & html) elements aren't closed.
您的代码看起来是正确的,但是 window.external.notify() 方法采用 String 参数,而不是 Integer。尝试调用 window.external.notify('123') 。
希望这有帮助。
Your code seems right, but window.external.notify() method is takes String parameter, not Integer. Try window.external.notify('123') call instead.
Hope this helps.
首先,您可能想阅读 Mike Taulty 对离线浏览器内容的评论。
http://mtaulty.com/CommunityServer/blogs/mike_taultys_blog/archive/2011/06/22/silverlight-html-and-the-webbrowser-control-for-offline-apps.aspx
First off you probably want to read what Mike Taulty comments on offline browser content.
http://mtaulty.com/CommunityServer/blogs/mike_taultys_blog/archive/2011/06/22/silverlight-html-and-the-webbrowser-control-for-offline-apps.aspx