SetVariable(“app”, this)


I have the following code in a Silverlight 3 application using Dynamic Language Runtime JavaScript ("button" is the name of a Button control):
private void Button_Click(object sender, RoutedEventArgs e)
var scriptRuntime = ScriptRuntime.Create();
foreach (var name in new[] { "mscorlib", "System", "System.Windows" })
var scriptEngine = scriptRuntime.GetEngine("js");
var baseScope = scriptEngine.CreateScope();
baseScope.SetVariable("app", this);
string script = "app.button.Content='Test'";
var scriptSource = scriptEngine.CreateScriptSourceFromString(script,
var compiledCode = scriptSource.Compile();
It throws "Object required" exception on Execute. But if I replace
baseScope.SetVariable("app", this);
string script = "app.button.Content='Test'";
baseScope.SetVariable("button", this.button);
string script = "button.Content='Test'";
it executes successfully and button.Content changes. It strangely seems like "button" is not accessible via "this". I guess any control in Silverlight is "public" so any class can use such child controls from this.GetType() instance. Please help with solving this.
Closed Jun 11, 2010 at 8:43 PM by billchi


dinov wrote Apr 30, 2010 at 8:11 PM

Can you include the source code for the class which has the button property or field? You should make sure that both the class and the field/property are public.

wrote Jun 11, 2010 at 8:43 PM

wrote Feb 21, 2013 at 10:41 PM

wrote May 16, 2013 at 10:29 AM