The first thing I will cover in the series about Kona is the good old DelegateCommand class, that all of the Prism veterans learned to respect. But first, a quick overview for those of you who never worked with commands before.
Why do we need commands for?
One of the corner stones of MVVM is clear separation of the view from the view model. Those two components should be as loosely coupled as possible and interact only by using binding. To accomplish this separation, the view model exposes it’s data with properties, and the view presents and manipulates that data by using the Binding markup extension. At runtime, the Binding expressions are resolved by inspecting the DataContext property of the control, which holds a reference to the view model (how the view model ends up being referenced in the control’s DataContext property is another issue which is solved elegantly by Kona. I will cover this in another post).
The following example shows a page with a TextBlock control with its Text property bounded to the view model’s CustomerName property:
And the corresponding view model:
This is the entire code. All the wiring is automatically done by the infrastructure. Note that the view and the view model does not know each other at all.
Binding supports a broader set of features. For example, the bounded element can be automatically updated given that the view model raises a specific event when the property changes (This also will be covered in another post).
Interaction with properties is only one side of the equation. What can you do if you need to invoke a method on the view model after the user performs an action in the view, for example, clicks a button? This is where you will use commands.
The ICommand interface
In XAML, each control that supports command invocation, such as ButtonBase and its derivatives, has the following additional dependency properties:
- Command: A reference to an ICommand object to invoke.
- CommandParameter: An optional parameter to pass to the ICommand object upon invocation.
In order to invoke a method on the view model when the user clicks a button, you need to create a new class, derived from the ICommand interface, create an instance of that class in your view model, expose it via a property, and then bind the Command property of the button to that property. The button will be enabled or disabled based on the command’s CanExecute method, and when the button will be clicked, the command’s Execute method will be invoked.
The following example shows a page with a Button control with its Command property bounded to the view model’s CreateCommand property:
And the corresponding view model:
Implementing the ICommand interface for each and every command in your view model is redundant, tedious and time consuming.
DelegateCommand and DelegateCommand<T>
These two simple classes, available in Kona, will save you a lot of time when you need to implement more than a few commands. The concept behind them is pretty simple. Instead of creating a new class for each command, just create an instance of the DelegateCommand class and pass it the Execute and CanExecute methods as delegates. Use the non-generic class for commands that do not receive parameter, and the generic class for commands that receive parameter. The parameter will be typed, unlike the nasty object parameter in the ICommand interface.
The following example shows the above view model, refactored to use a DelegateCommand:
Notice that you do not have to implement the CanExecute method for commands that can always be executed.
As you can see, using the DelegateCommand class is more elegant than re-implementing the ICommand interface. Next time I will walk you through Kona’s ViewModel class, and the ViewModelLocator class, which is responsible for the magic that automatically wires the views and the view models.
cross-posted from http://www.programmingtidbits.com/post/2013/01/18/Kona-Commands.aspx