简体   繁体   English

如何将数组从wpf控件传递到webbrowser控件中的javascript函数?

[英]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.

相关问题 如何将JSON数组传递给C#WebBrowser控件中的javascript函数? - How can I pass a JSON array to a javascript function in the C# WebBrowser Control? 在托管WebBrowser控件的应用程序中,如何在由WebBrowser控件查看的页面中调用JavaScript函数? - How might I call a JavaScript Function in a Page Viewed by a WebBrowser Control from the Application Hosting the WebBrowser Control? 如何在Webbrowser控件中调用javascript函数? - How can i invoke javascript function within the webbrowser control? 我如何将JavaScript事件从Webbrowser WPF控件发送到WPF的C#代码 - How do I send javascript event from webbrowser wpf control to c# code of wpf 如何从WPF WebBrowser控件调试页面的JavaScript代码 - How to debug JavaScript code of a page from WPF WebBrowser Control C#WPF WebBrowser-如何将数组传递给javascript函数 - C# WPF WebBrowser - how to pass an array to a javascript function 如何在WebBrowser控件中禁用JavaScript警告? - How can I disable JavaScript warnings in the WebBrowser control? 如何在WebBrowser控件中运行JavaScript? - How can I make JavaScript run in WebBrowser control? 如何将数据从WebBrowser控件中的JavaScript返回到C#? - How can I return data from JavaScript in a WebBrowser control to C#? MFC的WebBrowser控件—如何注入Java脚本? - WebBrowser Control from MFC — How to Inject Javascript?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM