Monday, February 20, 2006

Python & XAML
It seems like everyone has their Python-XAML mashup, I was just feeling left out. Building on my earlier fun extending XAML events using attached properties, I just had to make an action that executes some Python script:


<Rectangle Name='RedRect' Width='100' Height='66.6' Fill='Red'>
<evt:Triggers.Triggers>
<evt:PropertyTrigger
Trigger
='{Binding ElementName=RedRect, Path=IsMouseOver}' Value='True'>
<evt:PScriptlet xml:space='preserve'> <![CDATA[
if RedRect.RenderTransform.Value == Matrix.Identity:
RedRect.RenderTransform = RotateTransform()

RedRect.RenderTransform.Angle += 5
]]>
</evt:PScriptlet>
</evt:PropertyTrigger>
</evt:Triggers.Triggers>
</Rectangle>


This was my first stab at using the IronPython engine, it was definitely a very smooth experience. My favorite part is that I was able to get the items in the INameScope to register as variables in the script- from the Python, the named objects act like local variables. I'm still trying to figure out how to get some stuff (like adding dynamic properties to CLR objects), but it's definitely slick.

One of the downsides of having your UI defined in pure XML is that some of the glue code holding the parts together gets separated from the visual components. This is actually a design principle behind XAML- that it's completely skinnable when done properly. I have to say that I'm more of a fan of it's rapid development capabilities, brevity when setting up a complex UI and hierarchical format when representing nested components. Thus, the whole code-view separation thing sort of gets in the way for me- I just want to get something done and quick, when perusing the code later I only want to look in one place and I prefer source files to be as self-contained as possible.

I've been playing around with a ton of ideas of where the balance between XAML and C# should be, and have only succeeded in coming up with some fun samples projects- no answers. But at the least I have some executing code, it's so much fun to explore the boundaries.

Source (use project from here)