How should an array be passed to a Javascript function from C#?

后端 未结 4 967
误落风尘
误落风尘 2021-02-02 17:59

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         


        
4条回答
  •  你的背包
    2021-02-02 18:43

    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:

    function foo()
    {
        var stringArgs = [];
        for (var i = 0; i < arguments.length; i++)
            stringArgs.push(arguments[i]);
    
        // do stuff with stringArgs
    }
    

    And you'd call it from C# like this:

    List arguments = new List();
    arguments.Add("foo");
    arguments.Add("bar");
    webBrowser.InvokeScript("foo", arguments.ToArray());
    

提交回复
热议问题