views:

48

answers:

0

Hello folks. 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" })
    {
        scriptRuntime.LoadAssembly(scriptRuntime.Host.PlatformAdaptationLayer.LoadAssembly(name));
    }
    var scriptEngine = scriptRuntime.GetEngine("js");
    var baseScope = scriptEngine.CreateScope();
    baseScope.SetVariable("app", this);
    string script = "app.button.Content='Test'";
    var scriptSource = scriptEngine.CreateScriptSourceFromString(script,
        Microsoft.Scripting.SourceCodeKind.Statements);
    var compiledCode = scriptSource.Compile();
    compiledCode.Execute(baseScope);
}

It throws "Object required" exception on Execute. But if I replace


baseScope.SetVariable("app", this);
string script = "app.button.Content='Test'";

with


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. I would like to take the full control under "this" through DLR JS.