C# winform 支援html5的 控制項

來源:互聯網
上載者:User

標籤: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 
  1. private void Form1_Load(object sender, EventArgs e)  
  2. {  
  3.     WebBrowser wb = new WebBrowser();                      
  4.     wb.ObjectForScripting = new myClass();              
  5. }  
[csharp] view plaincopy 
  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 plaincopy 
  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 plaincopy 
  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,因為項目使用的網站用Firefox開啟是很快的,但是我找了幾天資料也沒發現怎麼傳遞個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 plaincopy 
  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 plaincopy 
  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 plaincopy 
  1. this.webKitBrowser1.GetScriptManager.EvaluateScript("var obj=window.external;");  
[csharp] view plaincopy 
  1. this.webKitBrowser1.GetScriptManager.EvaluateScript("var obj=window.external;");  

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

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

 

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

李民權

C# winform 支援html5的 控制項

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.