I’m working on a project in which we have to call some JS method of the containing page from a Silverlight application.

It seems to be really straight forward like:

1
HtmlPage.Window.Invoke("GlobalJsMethod",param1,param2);

But this works only for global methods and not for JS class methods.

In fact this will not work:

HtmlPage.Window.Invoke(“JsClassInstance.JsMethod”,param1,param2);

You can make a global wrapper for every class methods but you can also do something like this:
1
2
3
4
5
6
private ScriptObject _jsObject = HtmlPage.Window.Eval("JsClassInstance") as ScriptObject;
private void UserControl_Loaded(object sender, RoutedEventArgs e)
{
_jsObject.Invoke("JsMethod",param1,param2);
}

that is far more elegant.

This is only a quick tip. Maybe I’ll write a more detailed post an JS-Silverlight interaction in the future.