The solution: One Javascript function, and a call from Silverlight to the Javascript function. (Only working from within Internet Explorer)
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");
}
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.
December 26th, 2008 at 00:15
[...] 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 [...]