Html代码:
<script language="javascript" type="text/javascript">
<!-- 提供给C#程序调用的方法 -->
function messageBox(message)
{
alert(message);
}
</script>
winForm代码:
using System.Security.Permissions;
[PermissionSet(SecurityAction.Demand, Name = "FullTrust
[System.Runtime.InteropServices.ComVisibleAttribute(true)]
public partial class Form1 : Form
{
public Form1()
{
// 调用JavaScript的messageBox方法,并传入参数
object[] objects = new object[1];
objects[0] = "C#访问JavaScript脚本";
webBrowser1.Document.InvokeScript("messageBox", objects); //这里报错的
InitializeComponent();
}
}
运行后出现一下错误:
图片加不上...只能手打了...
未处理 NullReferenceException
未将对象引用设置到对象的实例。
编辑工具是VS2010
有知道解决办法的,麻烦帮忙解决一下~谢谢了
<script language="javascript" type="text/javascript">
<!-- 提供给C#程序调用的方法 -->
function messageBox(message)
{
alert(message);
}
</script>
winForm代码:
using System.Security.Permissions;
[PermissionSet(SecurityAction.Demand, Name = "FullTrust
[System.Runtime.InteropServices.ComVisibleAttribute(true)]
public partial class Form1 : Form
{
public Form1()
{
// 调用JavaScript的messageBox方法,并传入参数
object[] objects = new object[1];
objects[0] = "C#访问JavaScript脚本";
webBrowser1.Document.InvokeScript("messageBox", objects); //这里报错的
InitializeComponent();
}
}
运行后出现一下错误:
图片加不上...只能手打了...
未处理 NullReferenceException
未将对象引用设置到对象的实例。
编辑工具是VS2010
有知道解决办法的,麻烦帮忙解决一下~谢谢了
这个要怎么解决?我第一次接触这个控件
object[] objects = new object[1];
objects[0] = "C#访问JavaScript脚本";
webBrowser1.Document.InvokeScript("messageBox", objects); //这里报错的
InitializeComponent();WebBrowser 必须在Winform初始化之后才有效。
把你的代码(红色部分)放到 Form_Load 事件处理里,或者放在 InitializeComponent 之后。
正解,一般来说放Form_Load里面比较好,初始化的时候处理太多东西,界面显示就慢了
webBrowser1.Document.InvokeScript("messageBox", objects);
不过这句只有在按键事件里一直有用,在InitializeComponent();后面时还是会报空。然后在LOAD里时试了很多次,只有第一次成功,后面的全部都不显示ALERT了,不知道为什么,求解
现在还有个问题:
我这次要传个数组过去,这是winForm的代码
//调用JavaScript的messageBox方法,并传入参数
string[] a = { "aaa", "bbb", "ccc", "ddd" };
object[] objects = new object[a.Length];
for (int i = 0; i < a.Length; i++)
{
objects[i] = (object)a;
}
webBrowser1.Document.InvokeScript("messageBox", objects);
但是页面接收的时候我不知道怎么接收他了...
function messageBox(message)
{
var arr=new Array();
arr=message;
for (var i = 0; i < arr.Length; i++) {
Select1.options[i] = new Option(arr[i],i);
}
}
这样写报错,求解~~