2011-08-18 64 views
2

我在一个Silverlight应用程序中托管IronPython脚本,我想运行脚本并获取一个System.Windows.Controls.TextBlock对象。Silverlight和IronPython中的NotImplementedException

,所以我用这个代码的IronPython:

import clr 
clr.AddReferenceByName("System.Windows.Controls, Version=2.0.5.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35") 

from System.Windows.Controls import * 

tb = TextBlock() 

我被能够添加引用,但是,当我导入System.Windows.Controls我得到一个System.NotImplementedException。

同样的情况,如果我尝试“进口WPF”

我使用Silverlight 4中和IronPython 2.7.1β2 ,这是运行脚本代码:

Dim engine = IronPython.Hosting.Python.CreateEngine 
Dim scope = engine.CreateScope() 

Dim source = engine.CreateScriptSourceFromString(CodeTB.Text) 

source.Execute(scope) 
ResultLB.Items.Add(scope.GetVariable("hello")) 

If scope.ContainsVariable("tb") Then 
    GuiStack.Children.Add(scope.GetVariable("tb")) 
End If 

这里是异常的堆栈跟踪:

en Microsoft.Scripting.PlatformAdaptationLayer.FileExists(String path) 
en IronPython.Runtime.Importer.LoadModuleFromSource(CodeContext context, String name, String path) 
en IronPython.Runtime.Importer.LoadPackageFromSource(CodeContext context, String name, String path) 
en IronPython.Runtime.Importer.LoadFromDisk(CodeContext context, String name, String fullName, String str) 
en IronPython.Runtime.Importer.ImportFromPathHook(CodeContext context, String name, String fullName, List path, Func`5 defaultLoader) 
en IronPython.Runtime.Importer.ImportFromPath(CodeContext context, String name, String fullName, List path) 
en IronPython.Runtime.Importer.ImportTopAbsolute(CodeContext context, String name) 
en IronPython.Runtime.Importer.ImportModule(CodeContext context, Object globals, String modName, Boolean bottom, Int32 level) 
en IronPython.Modules.Builtin.__import__(CodeContext context, String name, Object globals, Object locals, Object fromlist, Int32 level) 
en Microsoft.Scripting.Interpreter.FuncCallInstruction`7.Run(InterpretedFrame frame) 
en Microsoft.Scripting.Interpreter.Interpreter.Run(InterpretedFrame frame) 
en Microsoft.Scripting.Interpreter.LightLambda.Run7[T0,T1,T2,T3,T4,T5,T6,TRet](T0 arg0, T1 arg1, T2 arg2, T3 arg3, T4 arg4, T5 arg5, T6 arg6) 
en IronPython.Runtime.Importer.ImportLightThrow(CodeContext context, String fullName, PythonTuple from, Int32 level) 
en IronPython.Runtime.Importer.Import(CodeContext context, String fullName, PythonTuple from, Int32 level) 
en IronPython.Runtime.Operations.PythonOps.ImportStar(CodeContext context, String fullName, Int32 level) 
en Microsoft.Scripting.Interpreter.ActionCallInstruction`3.Run(InterpretedFrame frame) 
en Microsoft.Scripting.Interpreter.Interpreter.Run(InterpretedFrame frame) 
en Microsoft.Scripting.Interpreter.LightLambda.Run2[T0,T1,TRet](T0 arg0, T1 arg1) 
en IronPython.Compiler.PythonScriptCode.RunWorker(CodeContext ctx) 
en IronPython.Compiler.PythonScriptCode.Run(Scope scope) 
en IronPython.Compiler.RuntimeScriptCode.InvokeTarget(Scope scope) 
en IronPython.Compiler.RuntimeScriptCode.Run(Scope scope) 
en Microsoft.Scripting.SourceUnit.Execute(Scope scope, ErrorSink errorSink) 
en Microsoft.Scripting.SourceUnit.Execute(Scope scope) 
en Microsoft.Scripting.Hosting.ScriptSource.Execute(ScriptScope scope) 
en TestApp2.MainPage.ExecuteButton_Click(Object sender, RoutedEventArgs e) 
en System.Windows.Controls.Primitives.ButtonBase.OnClick() 
en System.Windows.Controls.Button.OnClick() 
en System.Windows.Controls.Primitives.ButtonBase.OnMouseLeftButtonUp(MouseButtonEventArgs e) 
en System.Windows.Controls.Control.OnMouseLeftButtonUp(Control ctrl, EventArgs e) 
en MS.Internal.JoltHelper.FireEvent(IntPtr unmanagedObj, IntPtr unmanagedObjArgs, Int32 argsTypeIndex, Int32 actualArgsTypeIndex, String eventName) 

这里是所有source code

三江源一切:)

+0

什么方法会抛出异常? – svick

+0

@svick source.Execute(scope) – Victor

+0

我的意思是,最初抛出它的方法在堆栈跟踪的底部。其实,你能发布异常的整个堆栈跟踪吗? – svick

回答

0

谢谢你,卢卡斯,文章是非常有益的,现在的问题是固定的。 这里是我的新源代码

Dim setup As ScriptRuntimeSetup = Python.CreateRuntimeSetup(Nothing) 
setup.HostType = GetType(Microsoft.Scripting.Silverlight.BrowserScriptHost) 

Dim runtime As New ScriptRuntime(setup) 


Dim engine = IronPython.Hosting.Python.GetEngine(runtime) 

Dim scope = engine.CreateScope() 

Dim uri As New Uri("System.Windows.Controls.dll", UriKind.Relative) 
Dim sri As StreamResourceInfo = Application.GetResourceStream(uri) 

Dim ap As New AssemblyPart 
Dim asm As Assembly = ap.Load(sri.Stream) 
runtime.LoadAssembly(asm) 

Dim assamblies As String() = {"mscorlib", "System", "System.Windows", "System.Windows.Browser", "System.Net"} 

For Each item In assamblies 
    runtime.LoadAssembly(runtime.Host.PlatformAdaptationLayer.LoadAssembly(item)) 
Next 

Dim source = engine.CreateScriptSourceFromString(CodeTB.Text, Microsoft.Scripting.SourceCodeKind.Statements) 

source.Execute(scope) 
ResultLB.Items.Add(scope.GetVariable("hello")) 

If scope.ContainsVariable("tb") Then 
    GuiStack.Children.Add(scope.GetVariable("tb")) 
End If 
0

应该不会有任何AddReference

import clr 
from System.Windows.Controls import TextBlock 
tb = TextBlock() 
+0

是的,我试过了,但后来我得到一个IOException,消息说:“无法添加引用程序集System.Windows.Controls” – Victor

+0

好吧,我刚刚尝试过,没有需要为“TextBlock”添加引用。这对于Silverlight Toolkit控件是必要的,然后您需要将.kit工具包的.dll文件包含在.xap文件中。 –

+0

我得到了相同的NotImplementedException,我试着版本2.7和2.6.2但发生相同。 – Victor