I’ve forgotten most of what little I knew about Avalon aka Windows Presentation Foundation. So I spent a bit of time today trying to remember … here’s what I was able to accomplish:

OK, so that’s not really all that spectacular. What is spectacular, however, is how I can create this thing without having to define any CLR classes myself (which is impossible using my bridge). Here’s the XAML file:
<Window
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<StackPanel>
<Button Name="button1">Click me</Button>
</StackPanel>
</Window>
Note that this technique isn’t documented very well at all, but a bit of experimentation yielded this:
require 'wpf'
window = XamlReader.load(System::IO::File.open_read('app.xaml'))
button = window.find_name('button1')
button.click do |sender, args|
puts 'clicked me!'
end
Application.new.run(window)
The wicked-cool feature of this code is how XamlReader.load generates a Window object for me from the XAML. Now it’s a simple matter for me to find the button and hook up its Click event handler.
Now I’m not all that happy with the Ruby code. I really want it to look like this:
window = XamlReader.load(System::IO::File.open_read('app.xaml'))
window.button1.click do |sender, args|
puts 'clicked me!'
end
Application.new.run(window)
The window.button1 construct is much more Ruby-esque and requires that I hook method_missing in any class that derives from FrameworkElement to first try to call FindName after any other dynamic name resolution.
2 archived reader comments
Jason Bock (2006/03/09)
I’ve been very interested in your RubyCLR work, but I was wondering if you’ll have a "home page" for your bridge. A place where you have the code drops, maybe a list of related blog postings…I’m also asking because I don’t really have a good page to link to from my languages site :) : http://www.dotnetlanguages.net/DNL/Resources.aspx Keep up the good work. Regards, Jason
John Lam (2006/03/09)
Yes - after the next drop (hopefully this week) I’ll be building a new home for the bridge with a Trac install.