[IronPython] Question Regarding WPF : DataBinding, DataTemplates and C# Extensions

Michael Foord fuzzyman at voidspace.org.uk
Tue Nov 4 12:02:48 CET 2008


Simon Dahlbacka wrote:
> As the error message says, it cannot find the type LocalTimes.
>


You may be interested in DynamicScriptControl:

http://www.codeplex.com/dynamicscriptcontrol

It needs a bit (but not much) work to get it working with IronPython.

 From the project page:

We created a control that can be used with WPF or Silverlight to 
integrate DLR based controls into the xaml so that they can participate 
a little better into XAML based applications..


At this moment the control only supports IronRuby but that will soon change.

This project on codeplex will only hold releases if you're interested in 
the source code or contributing look for us at:
http://code.google.com/p/dynamic-script-control

I blogged about its usage: 
http://flanders.co.nz/2008/06/12/dynamic-script-control/

Michael

> Normally you have to map an xml namespace into the corresponding clr 
> namespace by adding something along the lines of
>
> xmlns:src="clr-namespace:Whatever.Namespace;assembly=TheAssemblyName"
>
> to the xaml file
>
> and then you would declare the objectDataProvider like this instead
>
>             <ObjectDataProvider x:Key="localTimes" ObjectType="{x:Type 
> src:LocalTimes}"/>
>
> However, I'm not sure how it works if you don't put LocalTimes in a 
> namespace
>
>
>
>
> On Mon, Nov 3, 2008 at 6:08 PM, Dino Viehland <dinov at microsoft.com 
> <mailto:dinov at microsoft.com>> wrote:
>
>     Declaring an interface from Python is easy:
>
>      
>
>     import System
>
>     class MyComparer(System.IComparable):
>
>         def CompareTo(self, other):
>
>             print 'compared'
>
>             return -1
>
>      
>
>     a = MyComparer()
>
>     b = MyComparer()
>
>     c = MyComparer()
>
>     l = System.Collections.ArrayList([a,b,c])
>
>     l.Sort()
>
>      
>
>     prints:
>
>      
>
>     compared
>
>     compared
>
>     compared
>
>     compared
>
>     compared
>
>      
>
>     Unfortunately I don't know enough about WPF to know why you'd get
>     the exception you're seeing.  One thing that might help would be
>     running with –X:ExceptionDetail so you get the full .NET stack
>     trace as well.  That might help someone else on the list identify
>     what's going on.
>
>      
>
>     *From:* users-bounces at lists.ironpython.com
>     <mailto:users-bounces at lists.ironpython.com>
>     [mailto:users-bounces at lists.ironpython.com
>     <mailto:users-bounces at lists.ironpython.com>] *On Behalf Of *Rob Oakes
>     *Sent:* Sunday, November 02, 2008 9:01 PM
>     *To:* users at lists.ironpython.com <mailto:users at lists.ironpython.com>
>     *Subject:* [IronPython] Question Regarding WPF : DataBinding,
>     DataTemplates and C# Extensions
>
>      
>
>     Dear Group,
>
>      
>
>     I have a rather involved question (more accurately series of
>     questions) regarding WPF, particularly how it relates to
>     DataBinding and DataTemplates.  I will do my best to keep this
>     posting to a reasonable length, but ... well ... I guess this
>     serves somewhat as a warning ;)  If you will spare me a bit of
>     background, I recently began trying to learn IronPython after
>     hearing an enthusiastic endorsement from a friend and colleague. 
>     While not a programmer or computer scientist by training (on the
>     opposite of the spectrum, actually, my degree is in biomedical
>     engineering); I have found myself increasing needing to work with
>     programming tools in the past few years.  This friend pointed me
>     toward the .Net framework, and IronPython in particular as a
>     potential solution to several major issues we have been facing
>     with our primary data processing tool: Matlab.
>
>      
>
>     As a trial test, I therefore decided to create a small program in
>     IronPython, namely, a Podcast Aggregator.  I purchased a Manning
>     Early Access Program copy of Michael Foord's (excellent BTW) book,
>     IronPython in Action and started trying to work toward my small
>     test project.  As I also wanted to learn WPF (not to mention make
>     my little program as pretty as possible), I decided to write the
>     user interface for my aggregator in .xaml.
>
>      
>
>     So, with that background out of the way, I arrive at a few
>     questions.  The first of these is somewhat of a follow-up to a
>     topic addressed in May on this very list somewhat recently
>     (reference:
>     http://lists.ironpython.com/pipermail/users-ironpython.com/2007-May/004912.html). 
>     In the original post, the author (Bish Bosh) asked what the best
>     method of referring to Python types in Xaml was.  Later in the
>     discussion, Dino Viehland offered that it might be best to:
>
>      
>
>     "Declare an interface in C# (or find an existing one which has
>     what you want) and inherit and implement the interfaces in
>     Python.  Then the interface will be the bridge between the static
>     and dynamic worlds" (reference:
>     http://lists.ironpython.com/pipermail/users-ironpython.com/2007-May/004913.html).
>
>      
>
>     So, my first question: After reading all the material I have been
>     able to find on the subject, I have not been able to locate a good
>     tutorial or example of how this might be done.  Might it be
>     possible to get some additional information on how this can be
>     accomplished?  A simple example of how it can then be integrated
>     into IronPython code would be extremely appreciated.
>
>      
>
>     I have made an effort using the information available in the
>     IronPython tutorial and a relatively simple XAML example
>     (reference:
>     http://blah.winsmarts.com/2007-3-WPF__The_DataTemplate,_choosing_how_your_data_will_look_like.aspx),
>     but I have had no luck in creating a test application that will
>     successfully load and perform as advertised.
>
>      
>
>     Using the xaml, C# and mainWindow.py file below, I receive the error:
>
>      
>
>     SystemError: Type reference cannot find public type named 'LocalTimes'
>
>      
>
>     I am at the end of my rope and am not sure what I should try
>     next.  Are there any ideas on how I might successfully implement a
>     custom C# interface?
>
>      
>
>     Sincerely,
>
>      
>
>     Rob Oakes
>
>      
>
>     PS, Here is the source code for the simple test case I have been
>     working with.
>
>      
>
>     << Main IPY script, called from command line with ipy TestApp.py
>
>      
>
>     import clr
>
>     import os
>
>     import WindowsRSS
>
>     clr.AddReference("PresentationFramework")
>
>     clr.AddReference("PresentationCore")
>
>     clr.AddReferenceToFile("IFancyData.dll")
>
>      
>
>     import LocalTimes, LocalTime
>
>      
>
>     from System.IO import File
>
>     from System.Windows.Markup import XamlReader from
>     System.ComponentModel import INotifyPropertyChanged from
>     System.ComponentModel import PropertyChangedEventArgs from
>     System.Windows import Application, Window
>
>      
>
>     class TestApp(object):
>
>      
>
>         def __init__(self):
>
>             xamlStream = File.OpenRead('DataBinding_20081102.xaml')
>
>             self.Root = XamlReader.Load(xamlStream)
>
>             self.LocalTimes = LocalTimes()
>
>      
>
>     mainWin = TestApp()
>
>     app = Application()
>
>     app.Resources.Add('collection', LocalTimes())
>
>     app.Run(mainWin.Root)
>
>      
>
>     >> 
>
>      
>
>     << DataBinding_20081102.xaml - GUI Window
>
>      
>
>     <Window
>     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
>
>         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
>
>         Title="Hello World!" Height="300" Width="300">
>
>           <Window.Resources>
>
>                 <ObjectDataProvider x:Key="localTimes"
>     ObjectType="{x:Type LocalTimes}"/>
>
>           </Window.Resources>
>
>           <Grid>
>
>           </Grid>
>
>     </Window>
>
>      
>
>     >> 
>
>      
>
>     << LocalTimes.cs - Custom C# Class - LocalTimes
>
>      
>
>     using System;
>
>     using System.Collections;
>
>     using System.Collections.Generic;
>
>      
>
>     public class LocalTimes : List<LocalTime> {
>
>      
>
>           public LocalTimes() {
>
>                 this.Add(new LocalTime("New York", DateTime.Now));
>
>                 this.Add(new LocalTime("Chicago",
>     DateTime.Now.AddHours(-1)));
>
>                 this.Add(new LocalTime("Denver",
>     DateTime.Now.AddHours(-2)));
>
>                 this.Add(new LocalTime("Los Angeles",
>     DateTime.Now.AddHours(-3)));
>
>           }
>
>     }
>
>      
>
>     public class LocalTime {
>
>      
>
>           private string place;
>
>           private DateTime time;
>
>          
>
>           public LocalTime(string _place, DateTime _time) {
>
>          
>
>                 place = _place;
>
>                 time = _time;
>
>           }
>
>          
>
>           public DateTime Time {
>
>                 get { return time; }
>
>                 set { time = value; }
>
>           }
>
>          
>
>           public string Place {
>
>          
>
>                 get { return place; }
>
>           }
>
>     }
>
>      
>
>     >> 
>
>      
>
>     Interpreter Output from the above files:
>
>      
>
>       File C:\Users\Rob
>     Oakes\AppData\IronPython\IPy-Tutorial\TestApp_20081102.py, line
>     23, in Initialize
>
>       File C:\Users\Rob
>     Oakes\AppData\IronPython\IPy-Tutorial\TestApp_20081102.py, line
>     20, in __init__
>
>       File , line 0, in Load##106
>
>       File PresentationFramework, line unknown, in Load
>
>       File PresentationFramework, line unknown, in XmlTreeBuildDefault
>
>       File PresentationFramework, line unknown, in Parse
>
>       File PresentationFramework, line unknown, in ParseFragment
>
>       File PresentationFramework, line unknown, in _Parse
>
>       File PresentationFramework, line unknown, in ReadXaml
>
>       File PresentationFramework, line unknown, in Read
>
>     SystemError: Type reference cannot find public type named
>     'LocalTimes'.
>
>
>     _______________________________________________
>     Users mailing list
>     Users at lists.ironpython.com <mailto:Users at lists.ironpython.com>
>     http://lists.ironpython.com/listinfo.cgi/users-ironpython.com
>
>
> ------------------------------------------------------------------------
>
> _______________________________________________
> Users mailing list
> Users at lists.ironpython.com
> http://lists.ironpython.com/listinfo.cgi/users-ironpython.com
>   


-- 
http://www.ironpythoninaction.com/
http://www.voidspace.org.uk/blog





More information about the Ironpython-users mailing list