Send byte array from javascript to silverlight
Can this be done ?
Below are snippets of what is not working, failing with exception:
function SendBytesJS() {
var control1 = documen开发者_如何学Got.getElementById('sl1');
bytes = new Array(1, 2, 3);
control1.Content.MainPage.SendBytesSL(bytes);
}
and
public partial class MainPage : UserControl
{
public MainPage()
{
InitializeComponent();
System.Windows.Browser.HtmlPage.RegisterScriptableObject("MainPage", this);
}
[System.Windows.Browser.ScriptableMember]
public void SendBytesSL(byte[] bytes)
{
// never gets here
}
}
The HTML bridge does not support byte arrays and Javascript only understands integer and float (actually double).
An array is passed as object[]
and numbers are always passed as double
. Hence your code needs to look more like the following:-
// Warning untested code
[ScriptableMember]
public void SendBytesSL(object[] arrayIn)
{
byte[] bytes = arrayIn.Select(o => Convert.ToByte(o)).ToArray();
}
精彩评论