将数组从 C# 传递到 Javascript

发布于 2024-11-26 23:51:26 字数 1585 浏览 1 评论 0原文

我有一个通过运行此方法来更新的处理程序:

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

只要我不尝试更改最后三个值,我就可以通过 Javascript 轻松地完成此

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

操作:这非常有效。

但是,我尝试使用 C# 通过非空值来完成此操作。我正在使用这段代码:

            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 是一个 int,radius 和 vel 是 float 数组,c 是一个 int 数组。

JCall 是我用来将 Javascript 添加到页面的方法。这对这个问题并不重要,并且它按预期工作。

这会导致 Javascript 错误:

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[]);

看起来它正在传递类型 (System.Single[]) 而不是数组本身。如何将数组传递给 Javascript?

更新:更明确地说,这里有三段代码。

C#,调用 JavaScript 函数。

JavaScript,调用处理小程序中的方法。

Java,一个具有“createParam”方法的处理小程序。

以下是 C# 中相关变量的声明方式:

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

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];

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

南巷近海 2024-12-03 23:51:26

我喜欢在 .Net http: //msdn.microsoft.com/en-us/library/system.web.script.serialization.javascriptserializer.aspx 只需注册客户端脚本 string.concat("var yourArray = ",序列化JsonString);那么 yourArray 对象将在 dom 中可用

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

写给空气的情书 2024-12-03 23:51:26

数组不会像您想象的那样转换为字符串。它们仅显示类型信息,这就是您看到 System.Single[] 的原因。相反,您必须使用 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);

还请注意,我用 [] 字符包围这些数组,因此它们在 Javascript 中被视为数组。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文