您的位置:首页 > Web前端 > JavaScript

c#winform中使用WebKit传递js对象实现与网页交互

2013-08-18 23:55 776 查看
有个项目要使用WebBroswer控件,并且要能传递一个js对象供前台调用,用c#的WebBroswer控件很容易实现:

[csharp]
view plaincopyprint?

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 可见:

[csharp]
view plaincopyprint?

[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控件,发现几个不错的替换控件:

GeokoFx:一个Firefox的Geoko引擎的Windows Forms包装,google上的下载地址:http://code.google.com/p/geckofx/ 官网:http://www.geckofx.org/
WebKit.NET:webkit的.NET封装,下载地址:http://sourceforge.net/projects/webkitdotnet/

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

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拖到你的窗体上.现在已经成功了一大步了,但是为了避免使用时遇到各种错误,我们需要先安装两个支持文件:

Microsoft C++ 2005 Redistributable http://www.microsoft.com/download/en/details.aspx?id=26347Windows XP/Vista/7 32/64 Bit
Apple QuickTime (Optional - for better HTML5 Support)

Ready!开始传递对象:

[csharp]
view plaincopyprint?

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调用,你也可以自己定义一个对象:

[csharp]
view plaincopyprint?

this.webKitBrowser1.GetScriptManager.EvaluateScript("var obj=window.external;");

this.webKitBrowser1.GetScriptManager.EvaluateScript("var obj=window.external;");

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

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

转载 http://blog.csdn.net/jallymn/article/details/8271671

李民权
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐