How do I bind to my DLR type?

Jun 15, 2009 at 11:16 PM
Edited Jun 16, 2009 at 2:32 AM

I'm just getting my feet wet with DLR in SL.  Basically I'm trying to define a class (that implements INotifyPropertyChanged) in python that I can new up and bind in native SL.

Here's my simple python class:
class pyclass:
    def __init__(self):
        self._seeds = "foo_pyclass"

    def __getSeeds(self):
        return self._seeds

    def __setSeeds(self,value):
        self._seeds = value

    Seeds = property(
        fget= __getSeeds,
        fset= __setSeeds
    ) 

My XAML is pretty trivial:
<Grid x:Name="LayoutRoot">
<TextBox x:Name="it" Text="{Binding Seeds}"></TextBox>
</Grid>

and my page's load:
Dim setup = Python.CreateRuntimeSetup(Nothing)
setup.HostType = GetType(Microsoft.Scripting.Silverlight.BrowserScriptHost)
setup.Options("SearchPaths") = New String() {String.Empty}

Dim runtime = New ScriptRuntime(setup)
Dim pe = Python.GetEngine(runtime)
' Load platform assemblies so you don't need to call LoadAssembly in script code.
runtime.LoadAssembly(runtime.Host.PlatformAdaptationLayer.LoadAssembly("mscorlib"))
runtime.LoadAssembly(runtime.Host.PlatformAdaptationLayer.LoadAssembly("System"))
runtime.LoadAssembly(runtime.Host.PlatformAdaptationLayer.LoadAssembly("System.Windows"))
runtime.LoadAssembly(runtime.Host.PlatformAdaptationLayer.LoadAssembly("System.Windows.Browser"))
runtime.LoadAssembly(runtime.Host.PlatformAdaptationLayer.LoadAssembly("System.Net"))

Dim scope = pe.CreateScope
Dim source = pe.CreateScriptSourceFromString(code, Microsoft.Scripting.SourceCodeKind.Statements)
source.Execute(scope)

Dim someClass = pe.GetVariable(scope, "pyclass")
Dim t = pe.Operations.CreateInstance(someClass)

LayoutRoot.DataContext = t

But, I'm not seeing it work. However, if I do the same with poco, it works fine:

Dim t as New poco()
LayoutRoot.DataContext = t

Public Class poco
    Public ReadOnly Property Seeds() As String
        Get
            Return "foo_poco"
        End Get
    End Property
End Class

 

 

Jun 16, 2009 at 6:18 PM

I just read this thread from '08 with a similar question for objects from Ruby: http://sdlsdk.codeplex.com/Thread/View.aspx?ThreadId=29531. However, I was hoping since that was a year ago, the ".net interop" work was done :)  This blog post shows it working from WPF http://krishnabhargav.blogspot.com/2009/02/binding-dynamic-types-in-wpf-using.html. So, what am I missing? Thank you.

Coordinator
Jun 24, 2009 at 1:28 AM

There is no difference between Silverlight and WPF WRT the major hosting API surface area. The article was written in February, and since then that method has been renamed to "CreateInstance": Operations.CreateInstance(someClass) will do the trick.

Jun 29, 2009 at 6:06 PM

Thank you for the reply. However, in my original code above, I am using the new "CreateInstance" but it's not working in Silverlight. It compiles, it runs, but it doesn't bind. Binding to a poco works fine.

I think I'm missing some basic point, but I can't see it. Note: In my code, I'm not running it through Chrion. My SL3 app just references the DL assemblies to be included. Also, in my code, the managed code is setting up the bindning which is slightly different than this example where the bidning is done in python.

http://devhawk.net/2009/04/24/clrtype+Metaclasses+Demo+Silverlight+Databinding.aspx

 

Any help appreciated.

Jun 29, 2009 at 6:17 PM

I believe the bit I'm missing pertains to __clrtype__ support to allow SL to reflect on the properties of my dynamic class.

http://devhawk.net/CategoryView,category,__clrtype__.aspx