2

i'm hosting IronPython Scripts in one Silverlight Application and i want to run the script and get one System.Windows.Controls.TextBlock object.

so i use this ironPython code:

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

from System.Windows.Controls import * 

tb = TextBlock()

i'm being able to add the reference, but, when i import System.Windows.Controls i get a System.NotImplementedException.

The same happens if i try with "import wpf"

i'm using Silverlight 4 and IronPython 2.7.1 beta2 and this is the code to run the script:

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

Here is the Stack Trace of the exception:

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)

And here is the all the source code

Thankyou for everything :)

Victor
  • 51
  • 1
  • 5

2 Answers2

0

It should work without any AddReference:

import clr
from System.Windows.Controls import TextBlock
tb = TextBlock()
Lukas Cenovsky
  • 5,476
  • 2
  • 31
  • 39
  • yes, i've tried, but then i got an IOException, the message says: "Could not add reference to assembly System.Windows.Controls" – Victor Aug 20 '11 at 16:11
  • OK, I've just tried it and there is no need to add reference for `TextBlock`. It would be necessary for Silverlight Toolkit controls and then you need to include toolkit's .dll in your .xap file. – Lukas Cenovsky Aug 21 '11 at 09:56
  • I got the same NotImplementedException, i tried with versions 2.7 and 2.6.2 but happens the same. – Victor Aug 22 '11 at 17:33
  • I suggest starting with the source generated by `IronPython-2.7\Silverlight\script\sl.bat python test`. It worked for me. Which reminds me - do you use special Silverlight .dlls, don't you? – Lukas Cenovsky Aug 22 '11 at 19:30
  • yes =p i use the Silverlight dlls. i tried the test app, but i can't see anything in the browser. – Victor Aug 22 '11 at 21:19
  • How do you test it in browser? What anything do you see? – Lukas Cenovsky Aug 22 '11 at 21:58
  • Sorry, i've forgot generate the .xap file, i tried with Chiron.exe but i get "Error occurred creating the configuration section handler for for Languages: Request failed". By the way, i tried mi project in another computer, but i received the NotImplementedException, also noted that at debug, the property SourceUnit from source variable haves: "The '?' character cannot be used here.", but i don't know if that's important. – Victor Aug 23 '11 at 04:50
  • Any code that does not uses from `System.Windows.Controls import <...>` works fine. – Victor Aug 23 '11 at 04:51
  • Looks like your source file encoding is wrong. Put somewhere all your source files and I check it. – Lukas Cenovsky Aug 23 '11 at 07:02
  • Unfortunately, I will not have time to look more thoroughly to your code and I am not so familiar with VB but it seems to me your IronPython hosting in Silverlight is wrong. Check this article: http://www.voidspace.org.uk/ironpython/silverlight/embedding_ironpython.shtml – Lukas Cenovsky Aug 23 '11 at 21:14
0

Thank You, Lukas, the article was very helpful, now the problem is fixed. Here's my new source code

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
Victor
  • 51
  • 1
  • 5