Silverlight and javascript

Communication between silverlight and javascript seems to be simple

To call a Javascript function from codebehind

HtmlPage.Window.CreateInstance(“FunctionName”) ;
or
//if the function has parameters
HtmlPage.Window.CreateInstance(“FunctionName”, “param1”, “param2”) ;

to call a method in codebehind from javascript

we have to decorate the method with “ScriptableMember”

 [ScriptableMember()]
 public string GetString()
 {
     return “this is a test”;            
 }

and in App.xaml.cs, we have to register our page

 private void Application_Startup(object sender, StartupEventArgs e)
 {
     Page page = new Page();
     HtmlPage.RegisterScriptableObject(“MyPage”, page);
     this.RootVisual = page;
 }
and in the html page where the control is hosted. we can call GetString() method defined in the page

function CallCodeBehind(){      
        var retvalue = document.getElementById(“SilverlightControl”).Content.MyPage.GetString());
        }

<div id=”silverlightControlHost”>
  <object data=”data:application/x-silverlight,”
            type=”application/x-silverlight-2-b1″ width=”400″
            height=”400″ name=”SilverlightControl”/></div>

Advertisements

2 thoughts on “Silverlight and javascript

    1. it should not be difficult, just copy the snippet of the code and paste it into one of the free online c# to vb converters, it will give the vb equivalent

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