views:

31

answers:

1

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?

+2  A: 

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 });
SLaks