Saturday, December 06, 2008

IronPython and [Insert MSFT Technology Here] (Starting with WPF)

Back in November (yes, I'm still behind on my posts but catching up), Harry Pierson (IPy PM) announced his decision to write a series of posts on how to use IronPython with a range of different Microsoft technologies:
Making good on his promise he starts with 6 posts on using the WPF (Windows Presentation Foundation) GUI toolkit with IronPython.

In the first part he starts a WPF based photo viewer for his wife's Windows Live photo collection.

"Turns out building this app in IPy was fairly straightforward, with a few pitfalls. I wasted half a day digging thru data binding before realized that data binding against IPy objects works out of the box - but only if you type the case of the property correctly (Title != title)..."

"Over the next several posts, I’m going to show you all the code for this app. It’s pretty small, only about 50 lines of app-specific python code + 50 lines of XAML to describe the window."
In part 2 Harry uses Expression Blend to design the User Interface and then loads the XAML it produces from IronPython:


def LoadXaml(filename):
from System.IO import File
from System.Windows.Markup import XamlReader
with File.OpenRead(filename) as f:
return XamlReader.Load(f)

He hooks up the events to the WPF Window loaded from the XAML and even shows a bit of C# with custom hooks to make it play nicely with IronPython.
Here’s the short version of this post: data binding in WPF to IPy objects just works... mostly. (For the longer version click though to the entry.)
Downloading the RSS feeds used by the WL Spaces Photo Viewer can take several seconds. Harry illustrates how to move functions into the background (not holding up the GUI thread) using a thread pool. To keep things clean (and Pythonic) he uses decorators to execute functions in the background or on the UI thread.
An alternative way of handling the background processing that uses SynchronizationContext instead of the WPF dispatcher.
Another change. The threading code is available for download from Harry's skydrive.
Implementing a REPL (Read-Eval-Print-Loop) so that he can poke around inside his application whilst it is running. This reuses components provided by IronPython to support the interactive interpreter.

No comments:

Post a Comment

Note: only a member of this blog may post a comment.