標籤:winform blog http io os 使用 ar for strong
OpenWebKitSharp
WebKit.net
c#winform中使用WebKit傳遞js對象實現與網頁互動分類: .NET開發2013-08-18 23:55 2496人閱讀 評論(1) 收藏 舉報
有個項目要使用WebBroswer控制項,並且要能傳遞一個js對象供前台調用,用c#的WebBroswer控制項很容易實現:
[csharp] view plaincopy
- private void Form1_Load(object sender, EventArgs e)
- {
- WebBrowser wb = new WebBrowser();
- wb.ObjectForScripting = new myClass();
- }
[csharp] view plaincopy
- private void Form1_Load(object sender, EventArgs e)
- {
- WebBrowser wb = new WebBrowser();
- wb.ObjectForScripting = new myClass();
- }
要傳遞的js對象必須使用[ComVisibleAttribute]標記為COM 可見:
[csharp] view plaincopy
- [System.Runtime.InteropServices.ComVisibleAttribute(true)]
- class myClass
- {
- public void Test()
- {
- System.Windows.Forms.MessageBox.Show("alert:Test");
- }
- }
[csharp] view plaincopy
- [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,因為項目使用的網站用Firefox開啟是很快的,但是我找了幾天資料也沒發現怎麼傳遞個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=26347WindowsXP/Vista/7 32/64 Bit
- Apple QuickTime (Optional - for better HTML5 Support)
Ready!開始傳遞對象:
[csharp] view plaincopy
- 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();
- }
[csharp] view plaincopy
- 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 plaincopy
- this.webKitBrowser1.GetScriptManager.EvaluateScript("var obj=window.external;");
[csharp] view plaincopy
- this.webKitBrowser1.GetScriptManager.EvaluateScript("var obj=window.external;");
這樣調用的時候就能用你自己定義的對象名訪問了。
應該也有直接自己定義對象的方法,但是open-webkit-sharp中文的資料實在的不多,耐著性子看了幾天老外的論壇,一水的全是吐槽,實際解決問題的不多。等有更好的方法,也請大家不吝賜教。
轉載 http://blog.csdn.net/jallymn/article/details/8271671
李民權
C# winform 支援html5的 控制項