Page 5 of 5
IronPython in action with WPF
IronPython works with WPF and XAML but it is still underdevelopment so there are parts that are supposed to work but don't.
To see this aspect of IronPython in action the best thing to do is to start a new WPF application project. As long as you are using IronPython Tools you will see the familiar drag-and-drop designer and the XAML editor.
The drag-and-drop designer is supposed to work but in the version of the Tools used for this article it didn't. However it was possible to enter XAML to create controls and then edit these in the designer by dragging and sizing.
To get started enter the following XAML into the editor:
At this point you might be tempted to try to work with names such as Button1 and TextBox1 but at the moment this doesn't work. IronPython works with uncompiled XAML and as such bindings between names in XAML and names in IronPython aren't made and so you can't simply use XAML assigned names in a IronPython program. It is possible to write some code that adds the names but this would take us too deep into the interaction between IronPython and other .NET languages and facilities.
The XAML is converted into a WPF object graph by the use of XamlReader:
XamlReader returns an object which is the root of the object graph i.e. a Window object in the case of the XAML code given above.
The controls are dynamically created as child nodes within the tree and to find a reference to any given control the simplest solution is to use the LogicalTreeHelper object and its FindLogicalNode method. This will return a reference to the control with a specific name that is a child of the initial control. So for example:
returns a reference to a child control of the window with the unique name 'Textbox1'. Once you have the reference to the control you can work with it as normal. For example:
The only problem with this approach is that you don't get any help from IntelliSense prompting for properties of the WPF controls.
As a more complete example consider the task of setting the Textbox's caption to "click" when the button is clicked. First we need to import all of the .NET support we need:
The click event hander is just:
from System.Windows.Markup import XamlReader
from System.Windows import *
from System.IO import FileStream, FileMode
Notice that you don't have to type the parameters - as long as you have the correct number for the event handler then everything just works.
Next we load the XAML and customise the controls:
Finally we set the .NET application running and this is done in exactly the same way as in most .NET projects - only in the case of C# and VB it is hidden from the programmer within generated code:
app = Application()
If you run the whole program you will be able to click the button and see the text in the Textbox change.
Python is an interesting language that has many powerful features that we haven't had space to touch on.
In particular you can use COM interop from IronPython and you can use Python libraries, two features which extend the language's usefulness.
IronPython is surprisingly good for a language that has been grafted onto .NET. It still has some potentially serious drawbacks - you can't use attributes and its integration with XAML isn't perfect. Better integration with Visual Studio or any IDE for that matter would make the whole thing easier to use and progress in this area has been slow and hampered by the need to keep up with .NET technology as it evolves.
It is worth knowing that there is a Silverlight project option within the Visual Studio Tools but at the moment creating Silverlight applications is just as ad-hoc as for WPF or Windows Forms.
IronPython also suffers from the usual problem associated with most open source projects, the documentation is terrible.There is plenty of documentation telling you about advanced features and even very simple features. What tends to be missing are the crucial pieces of information that you need to get started, such as how to wire up WPF event handlers, how to get at controls generated by Xaml and so on.
But even after all of these difficulties are taken into account it's an interesting example of a dynamic language running on the .NET platform - which after all was not designed for dynamic languages.