2

1 つの Silverlight アプリケーションで IronPython スクリプトをホストしており、スクリプトを実行して 1 つの System.Windows.Controls.TextBlock オブジェクトを取得したいと考えています。

だから私はこの鉄のPythonコードを使用します:

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 が発生します。

「import wpf」で試しても同じことが起こります

私は Silverlight 4 と IronPython 2.7.1 beta2 を使用しています。これはスクリプトを実行するコードです。

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)

そして、ここにすべてのソースコードがあります

すべてに感謝します:)

4

2 に答える 2

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
于 2011-08-24T05:05:24.333 に答える
0

なしで動作するはずAddReferenceです:

import clr
from System.Windows.Controls import TextBlock
tb = TextBlock()
于 2011-08-20T08:36:04.497 に答える