C# winform 支持html5的 控件


OpenWebKitSharp

WebKit.net

 

 

c#winform中使用WebKit傳遞js對象實現與網頁交互

分類: .NET開發

有個項目要使用WebBroswer控件,並且要能傳遞一個js對象供前台調用,用c#的WebBroswer控件很容易實現:

[csharp]  view plain copy
 
  1. private void Form1_Load(object sender, EventArgs e)  
  2. {  
  3.     WebBrowser wb = new WebBrowser();                      
  4.     wb.ObjectForScripting = new myClass();              
  5. }  
[csharp]  view plain copy
 
  1. private void Form1_Load(object sender, EventArgs e)  
  2. {  
  3.     WebBrowser wb = new WebBrowser();                      
  4.     wb.ObjectForScripting = new myClass();              
  5. }  

要傳遞的js對象必須使用[ComVisibleAttribute]標記為COM 可見:

[csharp]  view plain copy
 
  1. [System.Runtime.InteropServices.ComVisibleAttribute(true)]  
  2. class myClass  
  3. {  
  4.     public void Test()  
  5.     {  
  6.         System.Windows.Forms.MessageBox.Show("alert:Test");  
  7.     }  
  8. }  
[csharp]  view plain copy
 
  1. [System.Runtime.InteropServices.ComVisibleAttribute(true)]  
  2. class myClass  
  3. {  
  4.     public void Test()  
  5.     {  
  6.         System.Windows.Forms.MessageBox.Show("alert:Test");  
  7.     }  
  8. }  

這樣前台就能使用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=26347WindowsXP/Vista/7 32/64 Bit
  2. Apple QuickTime (Optional - for better HTML5 Support) 

Ready!開始傳遞對象:

[csharp]  view plain copy
 
  1. private void Form1_Load(object sender, EventArgs e)  
  2. {  
  3.     this.webKitBrowser1.Navigate("http://yourWebSiteUrl");  
  4.     this.webKitBrowser1.DocumentCompleted += new WebBrowserDocumentCompletedEventHandler(webKitBrowser1_DocumentCompleted);  
  5. }  
  6. void webKitBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)  
  7. {  
  8.     this.webKitBrowser1.GetScriptManager.ScriptObject = new myClass();  
  9. }  
[csharp]  view plain copy
 
  1. private void Form1_Load(object sender, EventArgs e)  
  2. {  
  3.     this.webKitBrowser1.Navigate("http://yourWebSiteUrl");  
  4.     this.webKitBrowser1.DocumentCompleted += new WebBrowserDocumentCompletedEventHandler(webKitBrowser1_DocumentCompleted);  
  5. }  
  6. void webKitBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)  
  7. {  
  8.     this.webKitBrowser1.GetScriptManager.ScriptObject = new myClass();  
  9. }  

前台調用方式類似IE的webbroswer,也使用window.external調用,你也可以自己定義一個對象:

[csharp]  view plain copy
 
  1. this.webKitBrowser1.GetScriptManager.EvaluateScript("var obj=window.external;");  
[csharp]  view plain copy
 
  1. this.webKitBrowser1.GetScriptManager.EvaluateScript("var obj=window.external;");  

這樣調用的時候就能用你自己定義的對象名訪問了。

應該也有直接自己定義對象的方法,但是open-webkit-sharp中文的資料實在的不多,耐着性子看了幾天老外的論壇,一水的全是吐槽,實際解決問題的不多。等有更好的方法,也請大家不吝賜教。

 

轉載  http://blog.csdn.net/jallymn/article/details/8271671

李民權


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM