在 SilverlIight 应用程序中执行 JScript
我尝试使用 Silverlight DLR 制作可编写脚本的应用程序。 当我尝试执行包含两行或更多行代码的 JScript 代码时,仅执行第一行。 例如: 我已经在 С# 上编写了编译的 SL 应用程序。 主页包含名为“lblMessage”的标签和带有 Click 事件处理程序的按钮。
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 将等于“第一行代码”字符串。
为什么忽略第二行代码?
谢谢。
I try to make scriptable application using Silverlight DLR. When I try execute JScript code which contains two or more lines of code, execution only first line. For example:
I had Compiled SL application written on С#. Main page contains Label named "lblMessage" and Button with Click event handler.
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);
}
After execution Label.Text will be equal "First line of code" string.
Why ignored second line of code ?
Thank you.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
也许您还应该在第二行末尾添加一个
;
(分号)...?Maybe you should also put a
;
(semi-colon) at the end of second line...?