At DevJam last year, I presented Dynamic UI in WinForms based on some things I had done on a client site during the year. A lot of people expressed an interest in the source code.

Magical.Trevor - An MVVM sample for WinForms

It has taken me a while but the source code that accompanies my DevJam presentation last December has finally been cleaned up enough to release into the wild. You can get it from http://github.com/wolfbyte/magicaltrevor

Magical.Trevor is an MVVM framework for WinForms based on some of the ideas found in Caliburn.Micro. The intent is not to create a robust framework that you drop into the core of your application and run your business on. Instead the idea is to get you thinking about removing the speed-bumps from your day to day development process by building up an architecture based on conventions.

Some highlights that I presented last year:

Getting started is easy

There is a Bootstrapper class. Create one and call it's .Run() method. Simple as that. I hate it when you get a new framework and you have to dig around to figure out how to get it started up. The flip-side is that you don't necessarily want a framework taking over your whole app. In this case I did want that, if you don't like it, write your own bootstrapper :) You can even base it on the one provided which offers a bunch of virtual methods for you to make different decisions during start-up.

Update the UI from any thread

I hate it when I run an app, spend 2-3 minutes getting into the state it needs to be at and then BOOM! it explodes because I forgot that I moved the update code to background thread and it's trying to mess with the UI (often not directly which makes it a total PITA). There is a static method called Execute.OnUiThread that accepts an Action and executes it on the UI thread. By default this class just executes the action passed in which means that classes that use it will still work even when not running in the UI (i.e. under test). The bootstrapper initializes the Execute static class to push actions to the UI thread if it is called from a background thread.

Updates to ViewModels are always on the UI thread

There is a class with the terrible name NotifiesOfPropertyChangesOnUiThread that serves as the base implementation for INotifyPropertyChanged on ViewModels. It automagically pushes property changed notifications up to the UI thread so now I don't have to worry about it. myVm.FirstName = "Mike"; just works again.

Conventional View Location

If I have a class called SearchViewModel and a User Control called SearchView it doesn't take a genius to figure out these belong together. Trevor finds these and wires them up for you. Even better, if you add a view model to your app for which no view can be found, Trevor will display it as a string explaining what the view should be called (namespace'n'all). Isn't that nice?

Conventional binding

Who likes editing the WinForms designer binding of the Text property on the FirstName TextBox to be bound to the FirstName property on their view model? Anyone?... No? Neither do I. If I have a string property with the same name as a TextBox, just wire them up. If I have a method with no parameters and the same name as a button, wire it up to the click event. And so on. I'm sure you can think of at least 7 more. And the system is extensible too. Just creating your own IBinder and register it in the bootstrapper.

Coming Soon

This version doesn't have the eventing system or the background task stuff that I showed at DevJam but it'll come (later when I've had sleep). The point is that there was a bunch of little things that really annoyed me when working in WinForms (or it might have just been CAB) and so I found ways to eliminate them.

Some time during the next week make it your mission to find something that annoys you about your current architecture and spend a lunch-time thinking about how you could make it go away. You may not have the opportunity to actually implement it but even thinking about it will make you feel better :)

P.S. In case you have never heard the reference: The original Magical Trevor - A mate and I once coded for around 10 hours straight in his lounge-room with Magical Trevor playing in the background the whole time. Needless to say, we were never quite the same. This code sample is named after it because everyone loves it. No cows were harmed in the making of this post.

Posted by: Mike Minutillo
Last revised: 02 Jun, 2011 03:20 PM History

Comments

No comments yet. Be the first!

No new comments are allowed on this post.