应该如何将数组从 C# 传递到 Javascript 函数?
我使用 WPF 中的 WebBrowser 对象,并在浏览器中加载的页面中调用一些 Javascript 代码,如下所示:
myWebBrowser.InvokeScript("myJsFunc", new object[] { foo.Text, bar.ToArray<string>()});
现在,js 函数应该迭代第二个参数(字符串数组)的元素并执行一些操作因此。唯一的问题是参数似乎没有作为 js 数组传递。
例如,
alert(typeof theArray);
警报“未知”。
从 CSharp 调用 js 函数时传递数组作为参数的正确方法是什么?
I use a WebBrowser object from WPF and I'm calling some Javascript code in the page loaded in the browser like this:
myWebBrowser.InvokeScript("myJsFunc", new object[] { foo.Text, bar.ToArray<string>()});
Now, the js function is supposed to iterate over the elements of the second parameter (an array of strings) and do stuff accordingly. The only issue is that the parameter seems not to be passed as a js array.
For example,
alert(typeof theArray);
alerts "Unknown".
What is the proper way to pass an array as a parameter when invoking a js function from CSharp?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
也许将其作为 json 字符串传递,并在 js 函数
js 中解析它:
http://blogs.microsoft.co.il/blogs/pini_dayan/archive/2009/03/12/convert-objects-to-json-in-c-using-javascriptserializer.aspx
Maybe pass it as a json string instead and parse it in the js function
js:
http://blogs.microsoft.co.il/blogs/pini_dayan/archive/2009/03/12/convert-objects-to-json-in-c-using-javascriptserializer.aspx
它本身并不能解决问题,但如果您只有一个数组要传递,它就可以解决问题:您可以向 JavaScript 函数发送任意数量的参数,并通过
arguments
特殊变量访问它们。它类似于接受可变数量参数的函数,具有相同的优点和问题(例如,您必须最后传递数组,并且如前所述,您只能传递一个)。下面是一个 JavaScript 函数示例:
您可以像这样从 C# 调用它:
It's not solving the issue itself but it solves the problem if you have only one array to pass: you can send an arbitrary number of parameters to a JavaScript function, and access them through the
arguments
special variable. It becomes analogous to a function accepting a variable number of arguments, with the same advantages and problems (for instance, you have to pass the array last, and as mentioned earlier you can only pass one).Here's an example JavaScript function:
And you'd call it from C# like this:
如果你真的想从代码中榨取每一点性能,你可以通过 javascript 中的 eval 来避免反序列化。这个概念是像这样构造调用:
请注意,我们使用 .execScript,这使得世界变得不同。这是因为与 .InvokeScript 相反,.InvokeScript 会强制使用基于字符串的参数(这迫使您在 javascript 端使用 eval),.execScript() 使我们能够编写任意代码javascript 代码,包括您在上面看到的内容(请注意,参数是显式 javascript 数组和一包属性)。现在我们可以直接编写数组和对象并将它们写为参数。为此,我们只需使用 Newtonsoft.Json 来序列化数组和对象:
另一件事是检索结果返回值:
为了方便起见,您可能需要编写一个实用程序方法,该方法迭代给定的参数并正确序列化它们,调用函数,然后检索返回值。
In case you really want to squeeze every bit of performance out of the code you can avoid the deserialization by means of eval inside javascript. The concept is to structure the call like this:
Notice that we use .execScript which makes all the difference in the world. This is because contrary to .InvokeScript which would forcefeed the javascript method with string based arguments (which is what forces you to use eval on the javascript side), .execScript() gives us the ability to write arbitrary javascript code including what you see above (notice that the arguments are an explicit javascript array and a bag of properties). Now we can directly code arrays and objects and write them as arguments. In order to do that we just use Newtonsoft.Json to serialize arrays and objects:
Another thing is retrieving the resulting returned value:
For your convenience you might want to write a utility method which iterates over the given parameters and serializes them properly, invokes the function and then retrieves the returned value.
您需要首先将数组转换为 JS 数组,如下所示:
下面是两个示例:
您可能还想删除尾随的 。不要忘记为StringBuilder导入适当的库,我认为是System.Text.StringBuilder;
或使用示例二:
You need to convert the array to a JS array first, something that looks like this:
Two examples follow:
You may want to remove the trailing , as well. Don't forget to import the appropriate libraries for StringBuilder, which I think is System.Text.StringBuilder;
or use, example two: