什么时候会出现winform应用程序内有HTML页面呢?
webBrowser控件内是可以打开HTML页面的
1、winform后台代码调用js脚本函数:
设置webBrowser可调用控件内的脚本代码,在构造函数内执行webBrowser.ObjectForScripting = this;
就能通过下面方法把数据传递到HTML页面并执行相关脚本函数
string name = "张三";
Action<string> actionSize = delegate(string username) //这里的username = name,不能同名
{
webBrowser.Document.InvokeScript("setUserName", new object[] { username}); //调用setUserName函数
};
webBrowser.BeginInvoke(actionSize, new object[] {string name });
2、js脚本调用C#后台代码方法:
首先,在C#后台代码类名上添加一句话。
//设置对Com可见
[System.Runtime.InteropServices.ComVisible(true)]
public partial class MainForm : Form
{
public string GetPath() {}
}
这个就可以在HTML页面通过var path = window.external.GetPath();调用C#后台GetPath()方法了。