do { Engine.BlogAbout(".NET","Silverlight"); } while ( alive );  

Mark Monster

Mark Monster  

Restarting the Silverlight Object with different initParams from within Silverlight

On the Silverlight forums I found out someone wants to refresh the Silverlight Object from Javascript. Basically this solution calls a Javascript function from the Silverlight environment. I though it would be a good way to make use of features available in Silverlight.js to make it easier to create the Javascript function to refresh the Silverlight Object. The strange thing was it took me a long time to get it to work. Yes this small part Javascript in the example below took me a very long time. This has all to do with using Google Chrome and Mozilla Firefox. In the end I was not able to get this working in Chrome and Firefox, I’m not sure if this is because of failing in Silverlight.js or not, but I was not even able to load an initial Silverlight Control from javascript.

The solution: One Javascript function, and a call from Silverlight to the Javascript function. (Only working from within Internet Explorer)

The Javascript function.

The function is very straight forward. I make use of the Silverlight.js function Silverlight.CreateObject. The function I created asks for three parameters, the hosting element (will contain the Silverlight control), the source (location of the xap file), and the initParams.

function CreateSilverlight(hostElement, source, initParams) { var pluginId = hostElement.id + "PluginId"; hostElement.innerHTML = Silverlight.createObject(source, null, pluginId, { width: "200", height: "50", background: "white", alt: "<!--Silverlight 2.not installed-->", version: "2.0.31005.0", autoUpgrade: true }, { onError: onSLError, onLoad: onSLLoad }, initParams, hostElement.id); }

Make it load from the window.load.

window.onload = function() { CreateSilverlight(silverlightControlHost, "ClientBin/MM.Silverlight.Experiments2.SUI.xap", "param1=value1,param2=value2"); }

The call from Silverlight.

I wanted to be able to see that the code was actually executed so I put a timestamp in. Calling Javascript from Silverlight is very easy, you can make use of either HtmlPage.Window.Eval or HtmlPage.Window.CreateInstance, I used HtmlPage.Window.Eval.

HtmlPage.Window.Eval(string.Format( @"CreateSilverlight(silverlightControlHost, 'ClientBin/MM.Silverlight.Experiments2.SUI.xap', 'currenttime={0},otherparam=othervalue');", DateTime.Now));

This is all, nothing more, the sad part I only got it working in Internet Explorer, but that might be because of some bad Javascript. I attached the full solution for download.

One Response to “Restarting the Silverlight Object with different initParams from within Silverlight”

  1. Silverlight Cream for December 22, 2008 -- #464 Says:

    [...] 10k Smart Coding Contest, and is shamelessly asking for our votes. From SilverlightCream.com: Restarting the Silverlight Object with different initParams from within Silverlight Mark Monster shows us how to restart our SL app with different init parameters, and he’s doing it [...]

Leave a Reply