【问题标题】:Execution JScript in SilverlIght Application在 SilverlLight 应用程序中执行 JScript
【发布时间】:2009-06-16 05:14:43
【问题描述】:

我尝试使用 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将等于“First line of code”字符串。

为什么忽略第二行代码?

谢谢。

【问题讨论】:

    标签: c# javascript silverlight dynamic-language-runtime


    【解决方案1】:

    也许您还应该在第二行末尾添加;(分号)...?

    【讨论】:

    • 我尝试这样做,并得到了 samr 结果。第二行被忽略。 string script = "lblMessage.Content = '第一行代码';\r\n" + "lblMessage.Content = '第二行代码;\r\n'";
    猜你喜欢
    • 1970-01-01
    • 2020-02-04
    • 1970-01-01
    • 2016-01-04
    • 2013-01-19
    • 2011-09-05
    • 1970-01-01
    • 1970-01-01
    • 2018-02-25
    相关资源
    最近更新 更多