简体   繁体   中英

Passing arrays from C# to Javascript

I have a Processing program that I update by running this method:

public void createParam (int nWidth, int nHeight, int nNumBalls, float[] nR, float[] nVel, int[] nC)...

I can do this through Javascript pretty easily as long as I don't try to change the last three values:

      var applet = document.getElementById("ballApplet");
      applet.createParam(divw, divh, 6, null, null, null); 

This works perfectly.

However, I'm trying to use C# to accomplish this with non-null values. I'm using this code:

            string command = "var applet = document.getElementById('ballApplet'); ";          
            command = command + "applet.createParam(divw, divh," + numBalls + ", " + r + ", " + vel + ", " + c + ");"; //width, height, num, radius, vel, c
            JCall(command);

num is an int, radius and vel are float arrays, and c is an int array.

JCall is a method I use to add Javascript to a page. It's not important to this problem and it works as intended.

This results in the Javascript error:

Error: syntax error
Source file: http://localhost:63803/Default.aspx
Line: 1227, Column: 214
Source code:
var applet = document.getElementById('ballApplet'); 
applet.createParam(divw, divh,1, System.Single[], System.Single[], System.Int32[]);

It seems like it's passing the type (System.Single[]) rather than the array itself. How do I pass the array to Javascript?

Update: To be a bit more explicit, there's three pieces of code here.

C#, that calls a JavaScript function.

JavaScript, that calls a method in a Processing applet.

Java, a Processing applet that has the "createParam" method.

Here is how the relevent variables are declared in C#:

            int numBalls = eventCount;
            ...
            float[] vel = new float[numBalls];
            ...
            float[] r = new float[numBalls];
            int[] c = new int[numBalls];

Arrays don't convert to strings like you're thinking. They just show the type information, which is why you're seeing System.Single[] . Instead, you'll have to do it yourself using string.Join :

string command = "var applet = document.getElementById('ballApplet'); ";          
command = command + string.Format(
    "applet.createParam(divw, divh, {0}, [{1}], [{2}], [{3}]);",
    numBalls, 
    string.Join(", ", r.Select(x => x.ToString())),
    string.Join(", ", vel.Select(x => x.ToString())),
    string.Join(", ", c.Select(x => x.ToString()))
);
JCall(command);

Notice also I'm surrounding those arrays with [ and ] characters so they are treated as arrays in Javascript.

I like using the json serializer in .Net http://msdn.microsoft.com/en-us/library/system.web.script.serialization.javascriptserializer.aspx just register the clientscript string.concat("var yourArray = ", serializedJsonString); then the yourArray object will be available in the dom

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM