[英]How can I pass array from wpf control to javascript function in webbrowser control?
I can send one argument, but can't send an array. 我可以发送一个参数,但不能发送数组。
C# code: C#代码:
string[] mes = {"Bascketball Stadium", "New Address", "Ilya"};
this.MyWebBrowser.InvokeScript("DaveWriter", mes);
JavaScript code: JavaScript代码:
function DaveWriter(mes){
var arr = jQuery.makeArray(mes);
document.getElementById('company').innerHTML = arr[0];
document.getElementById('address').innerHTML = arr[1];
}
I recieve only the 1st element of the array. 我只收到数组的第一个元素。
What should I change to recieve all elements? 我应该更改什么才能接收所有元素?
Very tricky because the InvokeScript takes the arguments as an array. 非常棘手,因为InvokeScript将参数作为数组。 So for example, you're really passing 3 parameters there, not an array of parameters, each of the elements of mes
would be one argument to DaveWriter
. 因此,例如,您实际上在其中传递了3个参数,而不是参数数组, mes
每个元素都是DaveWriter
一个参数。 Meaning, if you changed it to: 意思是,如果您将其更改为:
function DaveWriter(arg1, arg2, arg3) {
// Your code
}
It would be there. 在那里。 There isn't, from what I've seen, a clean method to do what you want. 从我所看到的情况来看,没有一种干净的方法可以执行您想要的操作。 Some recommend JSON, then evaluating the JSON in JavaScript. 一些建议使用JSON,然后在JavaScript中评估JSON。 I don't like that idea because you have to use eval()
which makes me feel wrong. 我不喜欢这个主意,因为您必须使用eval()
,这让我感到不对。
Here's what I suggest. 这是我的建议。 What if you pass it like you are, then access it via the arguments
variable. 如果按原样传递它,然后通过arguments
变量访问它,该怎么办? See Arguments object for more information. 有关更多信息,请参见Arguments对象 。 Basically: 基本上:
function DaveWriter(mes){
document.getElementById('company').innerHTML = arguments[0];
document.getElementById('address').innerHTML = arguments[1];
}
It's not ideal, but it will allow you to do what you want. 这不是理想的方法,但是它会让您做自己想做的事情。 I don't believe there are many other options. 我不相信还有很多其他选择。 Maybe someone knows of a cleaner way, but this should work for you. 也许有人知道更干净的方法,但这应该对您有用。 I didn't have an opportunity to test that fully as I don't have a WPF application with a WebBrowser handy, but I think that will accomplish what you need. 由于没有手持WebBrowser的WPF应用程序,因此我没有机会进行全面测试,但是我认为这可以满足您的需求。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.