开发者

Send byte array from javascript to silverlight

Can this be done ?

Below are snippets of what is not working, failing with exception:

Send byte array from javascript to silverlight

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();
 }
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜