我尝试使用Silverlight DLR制作可编写脚本的应用程序。当我尝试执行包含两行或更多行代码的JScript代码时,只执行第一行。例如: 我在С#上编写了Compiled SL应用程序。主页面包含名为“lblMessage”的Label和带有Click事件处理程序的Button。
private void Button_Click(object sender, RoutedEventArgs e)
{
ScriptRuntime scriptRuntime = ScriptRuntime.Create();
foreach (string name in new string[] { "mscorlib", "System", "System.Windows", "System.Windows.Browser", "System.Net" })
{
scriptRuntime.LoadAssembly( scriptRuntime.Host.PlatformAdaptationLayer.LoadAssembly(name) );
}
ScriptEngine scriptEngine = scriptRuntime.GetEngine("js");
ScriptScope baseScope = scriptEngine.CreateScope();
gsFormManager fm = gsApplicationManager.FormManager;
baseScope.SetVariable("lblMessage", lblMessage);
string script = "lblMessage.Text = 'First line of code';\r\n" + "lblMessage.Text = 'Second line of code'";
ScriptSource scriptSource = scriptEngine.CreateScriptSourceFromString(script);
CompiledCode compiledCode = scriptSource.Compile();
compiledCode.Execute(baseScope);
}
执行后Label.Text将等于“第一行代码”字符串。
为什么忽略第二行代码?
谢谢。
答案 0 :(得分:0)
也许你还应该在第二行末尾放一个;
(分号)......?