wpf内嵌html5,支持HTML5内嵌浏览器解决方案--OpenWebKitSharp

有个项目要使用WebBroswer控件,并且要能传递一个js对象供前台调用,用c#的WebBroswer控件很容易实现:

private void Form1_Load(object sender, EventArgs

e) { WebBrowser

wb = new

WebBrowser(); wb.ObjectForScripting = new

myClass(); }

private void Form1_Load(object sender, EventArgs

e) { WebBrowser

wb = new

WebBrowser(); wb.ObjectForScripting = new

myClass(); }

要传递的js对象必须使用[ComVisibleAttribute]标记为COM 可见:

[System.Runtime.InteropServices.ComVisibleAttribute(true)] class myClass { public void

Test() { System.Windows.Forms.MessageBox.Show("alert:Test"); } }

[System.Runtime.InteropServices.ComVisibleAttribute(true)] class myClass { public void

Test() { System.Windows.Forms.MessageBox.Show("alert:Test"); } }

这样前台就能使用window.external调用myClass的方法: window.external.Test();

如果就这样那就简单了

,可偏偏项目使用的网站对IE的兼容性极差(吐槽下:个人觉得是IE太烂了,对标准的支持太差),无奈之下想找寻其他类似的WebBrowser控件,发现几个不错的替换控件:

1.GeokoFx:一个Firefox的Geoko引擎的Windows Forms包装,google上的下载地址:http://code.google.com/p/geckofx/

官网:http://www.geckofx.org/

2.WebKit.NET:webkit的.NET封装,下载地址:http://sourceforge.net/projects/webkitdotnet/

本来决定使用GeokoFx,因为项目使用的网站用火狐打开是很快的,但是我找了几天资料也没发现怎么传递个js对象给控件,当发现Qt的webbroswer控件也是封装的WebKit控件时,遂决定使用WebKit,但WebKit.NET也没有直接提供传递对象的方法,后来发现又一个好东西:

1.open-webkit-sharp:对webkit.net的又一次封装,提供了很多新功能。google上下载地址:http://code.google.com/p/open-webkit-sharp/

下面的使用就非常简单了,下载open-webkit-sharp后,把Core文件夹和References文件夹下所有文件拷贝到你的工程目录下,然后打开你的项目,添加引用OpenWebKitSharp.dll和WebKit.Interop.dll(如果你的项目运行在.NET

Framework 2.0 或 3.5 引用

Binary_NET2文件夹下的这两个文件,NET4.0的话就引用Binary文件夹下的这两个dll);然后就是工具箱->选择项->选择OpenWebKitSharp.dll,然后从工具箱中把WebKitBrowser拖到你的窗体上.现在已经成功了一大步了,但是为了避免使用时遇到各种错误,我们需要先安装两个支持文件:

1.Microsoft C++ 2005 Redistributable http://www.microsoft.com/download/en/details.aspx?id=26347Windows

XP/Vista/7 32/64 Bit

2.Apple QuickTime (Optional - for better HTML5 Support)

Ready!开始传递对象:

private void Form1_Load(object sender, EventArgs

e) { this.webKitBrowser1.Navigate("http://yourWebSiteUrl"); this.webKitBrowser1.DocumentCompleted += new

WebBrowserDocumentCompletedEventHandler(webKitBrowser1_DocumentCompleted); } void webKitBrowser1_DocumentCompleted(object sender,

WebBrowserDocumentCompletedEventArgs e) { this.webKitBrowser1.GetScriptManager.ScriptObject = new

myClass(); }

private void Form1_Load(object sender, EventArgs

e) { this.webKitBrowser1.Navigate("http://yourWebSiteUrl"); this.webKitBrowser1.DocumentCompleted += new

WebBrowserDocumentCompletedEventHandler(webKitBrowser1_DocumentCompleted); } void webKitBrowser1_DocumentCompleted(object sender,

WebBrowserDocumentCompletedEventArgs e) { this.webKitBrowser1.GetScriptManager.ScriptObject = new

myClass(); }

前台调用方式类似IE的webbroswer,也使用window.external调用,你也可以自己定义一个对象:

this.webKitBrowser1.GetScriptManager.EvaluateScript("var

obj=window.external;");

this.webKitBrowser1.GetScriptManager.EvaluateScript("var

obj=window.external;");

这样调用的时候就能用你自己定义的对象名访问了。

应该也有直接自己定义对象的方法,但是open-webkit-sharp中文的资料实在的不多,耐着性子看了几天老外的论坛,一水的全是吐槽,实际解决问题的不多。等有更好的方法,也请大家不吝赐教。

李民权

From 华仔103: If it is not understand

enough, read the article as follow:

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值