Sunday, November 18, 2007

DLR Pad - interactive programming with XAML and DLR

This post is about a tool that I have build. It is called DLR Pad. In one sentence it allows you to rapidly create simple XAML based applications and script against them with dynamic language of your choice. DLR Pad was in my head for quite some time, but recently I had enough time to implement it. Let me explain how I came up with the idea.


As you maybe already know this summer I have been to Korea to participate in this year's Imagine Cup competition. Our team have build a mind mapping application - Bookvar. We believe that Bookvar is the next generation mind mapping tool. I encourage you to go there, download the application and send us your feedback; we are listening. In order to win the competition we tried to use all the latest Microsoft technologies. Unfortunately we did not qualify for the final round. Now a couple of months later we are highly motivated to make Bookvar a real product.

DLR - Dynamic Language Runtime

One of the technologies we have used in Bookvar is DLR - Dynamic Language Runtime. DLR is a set of classes and services that allow building of high performance dynamic languages on top of CLR. The beauty of DLR is both the common type system that is shared between all language implementations and in the .NET integration itself as well. This means that you can access the whole .NET Framework. Right now Microsoft is in process of implementing two languages on top of it - Python and Ruby, called IronPython and IronRuby. Both of them are pure open source projects and can be downloaded from here - IronPython, IronRuby. If you want to wake up your inner sleeping language geek I encourage you to check them out (IronRuby even accepts external contributions).

We have used DLR to create an interactive programming environment (Bookvar console) for interacting with our mind mapping model. You can check demo of it in this Channel 8 video. The code for the console is based on the DLRConsole sample here.

After the competition I wanted to extend the Bookvar console so that I can define XAML objects (similar to XAMLPad) and write code against them in one of DLR languages. This is how DLR Pad was born.


Let's look at the main window of the app:


At the top left you can see the interactive console. It will be our coding sandbox. On the right is the rendered XAML content. The XAML itself is defined in big text box in the bottom. In our case we have a simple button called 'button'. What is cool about it is that we can access the button in the console window using its name. For example we can change the button's text using this snippet of code:

button.Content = "Hello"

Actually we are using IronPython here. Note that when you type the '.' you will get an IntelliSense window that will list all available members on the Button type. Unfortunately the IntelliSense works only in IronPython. IronRuby is still in pre-alpha release so only part of the features are implemented right now.


Adding event handler to the button

Let's examine more complex scenario - adding click event handler to the button.

IronPython way
Here is the python code to do this:

def handle(*args) : button.Content = "IronPython"
button.Click += handle


IronRuby way
Here is the ruby equivalent to the same operation: { button.Content = "IronRuby" }


Note: In order to start coding in IronRuby you should pick it up from the combo in the upper left corner.

Each object that you defined in your XAML that has a Name (x:Name) automatically is available in the console window.

What's next?

I'm in the process of setup the code on CodePlex here. The console has some bugs that I'm aware of. I also have some ideas how to expand the application in the future - ability to load and use custom assemblies and other interesting stuff. Your feedback and suggestions are welcome as well.


Start experimenting and having fun with DLR Pad by downloading it from here.

Hope you will find the tool useful.

1 comment:

Unknown said...

very nice. I haven't learned IronRuby or IronPython yet, but this will be a good tool to explore them with.
thanks for making it available