2014-03-04 35 views
0

我试图执行在Silverlight的IronPython的脚本文件,但我发现了异常如何从silverlight 5执行ironpython脚本文件?

序列中没有匹配的元素

string importScript = "import sys" + Environment.NewLine + 
     "sys.path.append(r\"{0}\")" + Environment.NewLine + 
     "from {1} import *"; 

     // python script to load 
     string fullPath = @"c:\path\myModule.py"; 

     var engine = Python.CreateEngine(); 
     ScriptScope scope = engine.CreateScope(); 

     // import the module 

     string scriptStr = string.Format(importScript,Path.GetDirectoryName(fullPath),Path.GetFileNameWithoutExtension(fullPath)); 
     var importSrc = engine.CreateScriptSourceFromString(scriptStr, Microsoft.Scripting.SourceCodeKind.File); 
     importSrc.Execute(scope); 

     // now you ca execute one-line expressions on the scope e.g. 
     string expr = "functionOfMyModule()"; 
     var result = engine.Execute(expr, scope);` 
+0

请澄清错误代码 – BillyBigPotatoes

+0

序列中没有匹配的元素,这是例外 – Vijaykumar

回答

0

首先创建范围在这里......这样

public static ScriptScope GetModule() 
    { 
     var pyfile = "PythonFunction.py"; 
     ScriptEngine engine = new ScriptRuntime(DynamicEngine.CreateRuntimeSetup(true)).GetEngine("IronPython"); 
     var code = new XapVirtualFilesystem().GetFileContents(pyfile); 
     ScriptScope scope = engine.CreateScope(); 
     ScriptSource script = engine.CreateScriptSourceFromString(code, pyfile); 
     script.Execute(scope); 
     return scope; 
    } 

调用此方法在你想

蟒文件是遵循PythonFunction.py

进口CLR clr.AddReference( “System.Windows”) 从System.Windows导入应用

STR = Application.Current.RootVisual.FindName(“TextBox2中“).Text

def hello(str): print”Hello“+ str +”!欢迎来到IronPython的!” 回报‘财产以后形式铁的Python’

Application.Current.RootVisual.FindName("textBox1").Text=hello(Application.Current.RootVisual.FindName("textBox2").Text)