WebBrowser和Hosted Html信息互通
1.Hosted Html->WebBrowser
使用window.external.notify (<param>)在Html端,然后在WebBrowser端使用WebBrowser.ScriptNotify来接受数据.
2.WebBrowser->Hosted Html
webBrowser.InvokeScript("execScript", "history.go(-1)");
string height = (string) webBrowser.InvokeScript ( "eval", "document.body.offsetHeight.toString()" );
StringBuilder bldr = new StringBuilder();bldr.Append("var script = document.createElement('script');");bldr.Append("script.text = 'function cb () { "); bldr.Append("window.external.notify (\"this text was injected on the fly\");}';");bldr.Append("var headNode = document.getElementsByTagName('HEAD'); "); bldr.Append( "if (headNode[0] != null);headNode[0].appendChild(script); ;");bldr.Append("var element= document.getElementById('btn'); "); bldr.Append("if ( element != null ) element.onclick = cb;");webBrowser.InvokeScript("execScript", bldr.ToString());
以上为3个实例,具体请参照博客:
http://blogs.msdn.com/b/jaimer/archive/2011/02/07/communication-between-html-in-webbrowser-and-silverlight-app.aspx
同时
webBrowser.InvokeScript还可以单独接受string作为参数调用没有param的javascript方法.
TAG: