Tonight I presented for the Dayton .Net Developers Group on WPF. Unlike my previous Lap Around WPF @ CONDG which was meant as a beginners guide to WPF, this time we were skipping the basics and plunging into a framework I’ve dubbed MiniMVC.
MiniMVC is, primarily, a set of DependencyProperties and custom ICommand’s which allow you to specify a Controller for any FrameworkElement and/or Action in the logical tree. Unlike Dan Crevier’s D-V-VM pattern or Josh Smith’s MVC pattern implementations which rely on explicitly defining RoutedCommands for each action, binding them up manually, and other unpleasantries (in my humble opinion – these guys still know more than I) with this framework you can use any old class, with any old method, and start executing actions immediately. Here’s an example:
<Window x:Class=”PostDemo.Window1″
xmlns=”http://schemas.microsoft.com/winfx/2006/xaml/presentation”
xmlns:x=”http://schemas.microsoft.com/winfx/2006/xaml”
xmlns:mvc=”http://cromwellhaus.com/wpf/minimvc”
xmlns:demo=”clr-namespace:PostDemo”>
<mvc:MiniMVC.Controller>
<demo:Window1Controller/>
</mvc:MiniMVC.Controller>
<StackPanel>
<TextBox x:Name=”txtMessage”Text=”" />
<Button>
<mvc:MiniMVC.Action>
<mvc:ControllerAction Trigger=”Click”Action=”Echo”
Return=”{BindingElementName=txtResult,Path=Text}”>
<mvc:ParameterParameterName=”message”
Value=”{BindingElementName=txtMessage,Path=Text}” />
</mvc:ControllerAction>
</mvc:MiniMVC.Action>
Write Something
</Button>
<TextBlock x:Name=”txtResult” />
</StackPanel>
</Window>
Here’s what’s going on:
First, we have this class, Window1Controller, which we are attaching to the Window’s MiniMVC.Controller property.
Second, we are setting the MiniMVC.Action property on the Button to a ControllerAction triggered by the button’s Click event. When Click occurs the Action to be taken on the Controller is Echo. We want to pass into Echo the value of the txtMessage TextBox Text value as parameter message. Also, we’re specifying that the Return value from Echo should be applied to the Text property of the txtResult TextBox controller.
Cool? So what’s really going on? Well, MiniMVC is handling the most importantly the OnChange event handler for the Action DependencyProperty and using that to dynamically create an ICommand class called ActionCommand. It’s also looking up the Click RoutedEvent via Reflection and adding the ActionCommand’s Execute method as the event handler for the located Click RoutedEvent. There is ZERO code in my code-behind. The Designer has free-reign to apply the Controller and action to any RoutedEvent he/she chooses and we can all sleep well knowing our concerns are separated. A happy marriage in my book.
I must give a lot of credit to Rob Eisenberg’s super-awesome Caliburn project on which much of the Action dependency property is based. His framework is full featured to the tilt, but is a little over done to my liking with the extensive use of Dependency Injection. If you find MiniMVC useful, though, I highly recommend you watch for progress on Caliburn.
You can download the full solution, including demo’s, from the presentation, as well as the above example project here.
