开发者

Why do I only get first element in Javascript array passed from Silverlight?

开发者 https://www.devze.com 2023-01-20 13:39 出处:网络
I\'m attempting to pass out an array of strings from a Silverlight application to a Javascript function.However I only seem to get the first element of the array, rather than the whole array.I\'ve rep

I'm attempting to pass out an array of strings from a Silverlight application to a Javascript function. However I only seem to get the first element of the array, rather than the whole array. I've reproduced it with the simple code below:

Silverlight:

 string[] Names = new string[5];
 Names[0] = "Test1";
 Names[1] = "Test2";开发者_开发技巧
 Names[2] = "Test3";
 Names[3] = "Test4";
 Names[4] = "Test5";

 HtmlPage.Window.Invoke("PopulateNames", Names);

Javascript:

function PopulateNames(names)
{
    window.alert(names);
}

In this case I only ever see "Test1" with the above code, or "undefined" if I replace window.alert(names) with window.alert(names[0]).

Does anyone know how I should be doing this to get all the elements out to the Javascript function?


The Invoke method takes an array of parameters.
Therefore, your five strings are being passed as five string parameters to the function.

You need to pass a nested array, like this:

HtmlPage.Window.Invoke("PopulateNames", new object[] { Names });


There is another property of a javascript function object. Its arguments. You can access the complete object array you pass through this arguments object. Try debugging your script and you will understand what I say. If you access the argument array of the function like 'funcation(myargs[])', the myargs will refer to the first value in the passed in array.

0

精彩评论

暂无评论...
验证码 换一张
取 消