Monday, June 14, 2010

Silverlight & Javascript Communication (Part 1)

Call Javascript Methods from within Silverlight

(http://pietschsoft.com/post/2008/06/Silverlight-and-JavaScript-Interop-Basics.aspx)

Using the Eval method (vulnerable to script injection)

using System.Windows.Browser;


HtmlPage.Window.Eval("myJSObject = 2;"); // set global parameter
HtmlPage.Window.Eval("myJSMethod();");

Using the Invoke method

using System.Windows.Browser;

HtmlPage.Window.Invoke("myJSMethod"); // no arguments
HtmlPage.Window.Invoke("myJSMethod", "Chris", 42); // pass in 2 args

Return values from Javascript method calls to Silverlight

// Returning a Double
double doubleValue = (double)HtmlPage.Window.Invoke("myJSMethod");

// Returning a String
string stringValue = (string)HtmlPage.Window.Invoke("myJSMethod");

Return objects from Javascript method calls and accessing its methods & properties
Handling ScriptObjects

Related:

Call Silverlight methods from Javascript
Handling ScriptObjects

No comments:

Post a Comment