我想實(shí)現(xiàn) js調(diào)用 winform里面的方法,通過webbrower控件和winform進(jìn)行交互。不知道怎么弄啊。
我在網(wǎng)上找了些資料,說的在js里面寫 window.external.方法名();
但是沒用啊,js報(bào)錯(cuò)。。。
this.webBrowser1.Url = new Uri(GetDefaultUrl());
C# code
using System;
using System.Windows.Forms;
using System.Threading;
using System.Runtime.InteropServices;
namespace WindowsFormsApplication1
{
[System.Runtime.InteropServices.ComVisibleAttribute(true)]
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
webBrowser1.ObjectForScripting = this;
}
public void Func(string str)
{
MessageBox.Show(str);
}
private void Form1_Load(object sender, EventArgs e)
{
webBrowser1.Navigate("about:blank");
webBrowser1.Document.Write("<script> function ButonClick() { window.external.Func('test');}</script><input type='button' onclick='ButonClick();'>test");
}
}
}
設(shè)置下面兩個(gè)地方
C# code
[System.Runtime.InteropServices.ComVisibleAttribute(true)]
public partial class Form1 : Form
webBrowser1.ObjectForScripting = this;