Mariusz, Gorzoch tech Blog

Archive for the ‘SilverLight’ Category

SilverLight to JavaScript

leave a comment »

Did you know that you can really easy expose methods from your SilverLight classes to Javascript? I didn’t, but I know now that this is really simple. Here is what you need to do in order to achieve that:

1. Mark the class you want to expose from SilverLight with [ScriptableType]

2. Mark method you want to expose from you class with [ScriptableMemeber]

3. Inside you class constructor you need to call :

HtmlPage.RegisterScriptableObject("bridge", this);

with will register you time in the plug-in and makes it by this available to JavaScript.

and you are almost done! The rest what you need to do is to call it properly from you JavaScript site. Bellow you can find simple snippet you can use for that:


You can see above method “buttonClick”. This method get instance of the plug-in (make sure to use id of the object which hosts your SilverLight app) and then call “content” method and on that method call the type we used in registration (=bridge). This give us instance of our class and now you can call your method. In my case signature of the method is like this:

public void ChangeLabel(string kto)
    label3.Content = kto;

but you can use as many arguments as you need.


Written by Mariusz Gorzoch

20 November 2011 at 16:04

Posted in SilverLight