1.工作 一年多了,一直沒對自己在工作遇到的問題進行總結,每次遇到問題都要在網上找資料,導致完成項目之后,時間久了就會生疏。所以下定 決定總結自己在工作中遇到的各種問題。
進入正題:第一次寫還請大神多多包涵,有不對的地方也請指點指點。
我在項目中 使用的是open-webkit-sharp:它是對webkit.net的又一次封裝,提供了很多新功能。
google下載地址 :https://code.google.com/archive/p/open-webkit-sharp/
github網址 : https://github.com/Erls-Corporation/open-webkit-sharp
開發工具:Vs2017
下載之后 解壓壓縮包. 把Core文件夾和References文件夾下所有文件拷貝到你的工程目錄(bin)下,然后打開你的項目,添加引用OpenWebKitSharp.dll和WebKit.Interop.dll(如果你的項目運行在.NET Framework 2.0 或 3.5 引用 Binary_NET2文件夾下的這兩個文件,NET4.0的話就引用Binary文件夾下的這兩個dll);
到自己的WinForm程序,在工具箱選擇按右鍵-選擇項
點擊瀏覽選擇OpenWebKitSharp.dll,然后把WebKitBrowser拖到winfrom窗體里。
想要html頁面訪問后台 必須在前面加上
//當前類可以com組件的形式供外包調用
//[ComVisible(true)]
//[PermissionSet(SecurityAction.Demand, Name = "FullTrust")]
[PermissionSet(SecurityAction.Demand, Name = "FullTrust")]
[System.Runtime.InteropServices.ComVisibleAttribute(true)]
winfrom后台程序
public class myClass
{
public int Test(int a ,int b)
{
//System.Windows.Forms.MessageBox.Show("alert:JS回調成功");
int c = a * b;
return c;
//System.Windows.Forms.MessageBox.Show("JS回調值alert:"+c);
}
}
private void Form1_Load(object sender, EventArgs e)
{
webKitBrowser1.Navigate("url");
this.webKitBrowser1.DocumentCompleted += new WebBrowserDocumentCompletedEventHandler(webKitBrowser1_DocumentCompleted);
}
public void webKitBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
{
this.webKitBrowser1.GetScriptManager.ScriptObject = new myClass();
}

1 public class myClass 2 { 3 public int Test(int a ,int b) 4 { 5 //System.Windows.Forms.MessageBox.Show("alert:JS回調成功"); 6 7 int c = a * b; 8 return c; 9 //System.Windows.Forms.MessageBox.Show("JS回調值alert:"+c); 10 } 11 12 } 13 private void Form1_Load(object sender, EventArgs e) 14 { 15 webKitBrowser1.Navigate("url"); 16 this.webKitBrowser1.DocumentCompleted += new WebBrowserDocumentCompletedEventHandler(webKitBrowser1_DocumentCompleted); 17 18 } 19 public void webKitBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e) 20 { 21 this.webKitBrowser1.GetScriptManager.ScriptObject = new myClass(); 22 23 }
html里的調用代碼
window.external.后台調用的方法