Silverlight communication with Javascript<!-- --> | <!-- -->Patrick Desjardins Blog
Patrick Desjardins Blog
Patrick Desjardins picture from a conference

Silverlight communication with Javascript

Posted on: October 16, 2011

You may have some situation that you need to communicate with the page that your Silverlight reside.

This can be done by three methods:

  • Passing parameter to Silverlight
  • Silverlight call a Javascript method of your page
  • The Javascript call a Silverlight method

Silverlight parameter

All Silverlight applications contain a startup method that has a StartupEventArgs. This class has a InitParam dictionary (IDictionary) that contains all parameters.

1private void Application_Startup(object sender, StartupEventArgs e) {
2 if (e.InitParams.ContainsKey("debug")) {
3 Debug.WriteLine(e.InitParams["debug"]);
4 }

To pass parameters you need to add in the html a new element with the attribute name "initParams".

1<div id="silverlightControlHost">
2 <object data="data:application/x-silverlight-2," type="application/x-silverlight-2" width="100%" height="100%">
3 <param name="source" value="ClientBin/MyProject.xap"/>
4 <param name="onError" value="onSilverlightError" />
5 <param name="background" value="white" />
6 <param name="minRuntimeVersion" value="4.0.50826.0" />
7 <param name="autoUpgrade" value="true" />
8 <a href="" style="text-decoration:none">
9 <img src="" alt="Get Microsoft Silverlight" style="border-style:none"/>
10 </a>
11 <param name="initParams" value="debug=false, secondParameter=2 " />
13 </object>
14 <iframe id="_sl_historyFrame" style="visibility:hidden;height:0px;width:0px;border:0px"></iframe>
15 </div>

As you can see in this example, if you need more than one parameter you need to use a comma between all parameters. The value is automatically a string. You will need to parse it to the desired type inside Silverlight.

Silverlight communicate with Javascript

The communication between Silverlight and Javascript is straight forward. You need to use the Invoke method of HtmlPage.Window.

1System.Windows.Browser.HtmlPage.Window.Invoke("MyFunction", "Parameter1");
2 System.Windows.Browser.HtmlPage.Window.Invoke("MyFunction", "Parameter1", "Parameter2");

The invoke method has a params[] for its second parameter. That let you add has many parameter value that the function in Javascript require.

1public virtual object Invoke(string name, params object[] args);

Javascript to communicate with Silverlight

Javascript can also call method inside a Silverlight application. To do, you need to first declare an entry point name.

1System.Windows.Browser.HtmlPage.RegisterScriptableObject("Page123", this);

This code can be added at the beginning of the constructor of the method that will be called. It specifies a unique identifier that will be used in the Javascript.

The second thing that need to be done inside Silverlight is to add an attribute to the method that you want to expose to the external.

2public void JavascriptWillCallThisMethod(string textFromJavaScript) {
3 Debug.WriteLine(textFromJavaScript);

The third and last step is to call this method from the Javascript code.

1var control = document.getElementById("silverlightControl");
2control.Content.Page123.JavascriptWillCallThisMethod("Hello from Javascript!");

As you can see, you find the "Page123" again here. It's the name declared in the Silverlight application when calling the RegisterScriptableObject method.