我正在尝试在 silverlight 中执行 ironpython 脚本文件,但出现异常
序列不包含匹配元素
string importScript = "import sys" + Environment.NewLine +
"sys.path.append( r"{0}" )" + Environment.NewLine +
"from {1} import *";
// python script to load
string fullPath = @"c:pathmyModule.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);`
首先在这里创建范围...喜欢这个
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;
}
在所需的位置调用此方法
Python 文件遵循 PythonFunction.py
导入 CLRCLR.AddReference("System.Windows")从系统.视窗导入应用程序
str=Application.Current.RootVisual.FindName("textBox2").发短信
def hello(str): 打印"你好"+ str + "!欢迎来到IronPython!" 返回"索姆形态铁蟒"
Application.Current.RootVisual.FindName("textBox1").Text=hello(Application.Current.RootVisual.FindName("textBox2").Text)