Gorgo.Live.ToString()

Mariusz, Gorzoch tech Blog

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:

image

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.

Advertisements

Written by Mariusz Gorzoch

20 November 2011 at 16:04

Posted in SilverLight

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: