Goodbye XAML, Hello AvalonBuilder
I don’t have to pay the angle-bracket tax anymore![1]
In all of my RubyCLR WPF/Avalon samples, I was doing an HTML-esque style of programming where I was injecting XAML into an Avalon program as text/XML. I even created the equivalent of an innerHTML property to support my HTML habits.
However, since Ruby’s syntax is so flexible, I decided to see if I could implement something akin to Jim Weirich’s most excellent Builder that is used to create XML documents.
During a break at the Lang .NET symposium this week, I hacked out a proof-of-concept internal DSL for generating Avalon element trees. I grabbed Wilco Bauwer who was also in the audience (it’s hard to miss Wilco since he’s 6’7” tall!) and had him be a human Ruby compiler to see if my idea would work. It looked good to both of us, so I decided to implement it. After some hacking in my hotel room tonight, this is what I came up with:
x = Builder::AvalonBuilder.new
x.Window {
x.FlowDocument {
x.Section {
output = x.Paragraph('your output goes here', :font_size => 30)
x.Paragraph(:font_family => FontFamily.new('Consolas')) {
x.text! "This is text in consolas"
x.Button { |b|
x.text! "click me"
b.click do |sender, args|
output.add_child(Run.new('clicked me!'))
end
}
}
}
}
}
Application.new.run(x.result)
The really cool thing about this is how I can have both the definition of the object model and executable Ruby code all packaged in an easy-to-read format. Notice how my event handler can reference the output Paragraph element.
Oh, and AvalonBuilder is implemented in around 70 lines of code
Is this sweet or what???!
[1] I coined this term when I crashed Scott Hanselman’s birds-of-a-feather session on code generation at some Tech Ed a few years ago. I was in an XML hating frame of mind that evening, lots of people were there, and apparently that term has stuck.


01. Aug, 2006 







Now that is just awesome! How do you find time for all this stuff? Great.
Builders are in the air apparenlty! That is great looking
_why does some stuff like this as well in his Camping framework (markaby), I think. I’m definitely not a Ruby guru yet, but I think his version uses “instance_eval” instead of “eval” so you don’t have to have the “x.” in front of everything.
Cool stuff.
Heh.
Apparently underscore starts italics.
Looks awesome John. Does this mean I can do event bubbling on the fly in ruby versus having to do it in xaml (which wasn’t possible before)?
This has gotten much better in more recent builds. All of the nasty x. stuff has been removed. I’m using instance_eval now, just like I do in … RubyCLR! More once I have some cycles to blog / upload photos …
Re: InstanceEval: Sweet. I was listening to a podcast with Jim Weirich and he said people just like the explicit-ness of the way builders work now, and originally he was actually using instance_eval. I gotta disagree tho’, it’s just cruft methinks for the most part.
This approach could also be used to generate XUL for mozilla-based apps, no? Or is XUL already easy enough to do by hand that it would be superfluous?
Phil, XUL is still XML, so it’s still painful to use. That and the ability to mix Ruby code inline with the rest of the element tree is what makes this approach really powerful.
Brian, this was a bit of inspiration that I got from Anders’ XLang talk. I hacked it up that evening in the hotel room …