Code for the SLAMD session tonight

Tonight, I presented a talk about MVVM “in the real world” at the Silverlight and Mobile Developer user group on Wellington.

For those of you who were there, I am sorry that it wasn’t better organized. I just ended up with way too much work the last 2 weeks, so I didn’t have enough time to structure it all enough. I decided that it was more important to get the code built than to have a  lot of PowerPoint slides.

As I did promise, I you can download the code below and I have also added the IDispatchService that I talked about as well. It will give you the ability to marshal execution back to the UI thread from a ViewModel. Just call IDispatchService.BeginInvoke() and pass in a lambda expression or a method to execute on the UI thread.

For those of you who weren’t at the user group, or were there and thought that what I said was completely impossible to understand, I just want to explain what is in the download.

The download contains 2 projects that together form a tiny MVVM toolkit. The two projects are called SLAMD.MVVM.Common and SLAMD.MVVM.Toolkit.

The Common namespace contains a couple of interfaces for the IDispatchService, IMEssageService and INavigationService. This assembly can be referenced by any assembly that will contain custom implementations for the services. The Toolkit project contains implementations for the interfaces as well as a bunch of base classes and helper classes.

The BackgroundExecution class offers help when it comes to running code on a background thread.

The DelegateCommand class is an ICommand implementation that executes by delegating to the Action you have defined. It also supports changing the “executability” by setting a bool property. This makes it possible to disable buttons and so on by setting it to not executable. It comes in 2 version, one generic and one regular. The generic version will make sure that the command parameter is cast to the defined type before executing.

The StateControl makes it possible for you to show different views/states in your UI by changing a property in the ViewModel. Just replace its template and include a VisualStateManager with as many states as you require. Then bind the StateControl’s State property to the ViewModel’s property. As soon as the ViewModel’s property is changed, the control will try to move to a visual state with the corresponding name.

The UnityViewModelBase acts as a base class for a ViewModel locator. It uses a IUnityContainer to support dependency injection for the ViewModels. Just inherit from it and expose a property for each ViewModel type needed. In the getter, just return Resolve<type>(). Then create an instance in the App.xaml’s Resources dictionary. And finally, in the Application_Startup method, get hold of the ViewModelLocator in the Resources dictionary and set its IUnityContainer by calling SetContainer().

The final piece of the “toolkit” is a ViewModelBase class, which is not surprisingly a base class for ViewModels. It implements the INotifyPropertyChanged interface using an implementation I have written about before.

If all of this seems hard to understand and weird, just open some of the other projects in the download. They form an application with views, services and viewmodels that make use of all of these features. It also uses the CommandManager, that I have written about numerous times before, to call some ICommands without having to use ButtonBase.

If you have any questions about what I talked about, don’t be afraid of asking me. Just add a comment or send me an e-mail at chris@59north.com.

Here is the code: SLAMD.MVVM.zip (279.61 kb)

Comments (1) -

Thanks Chris - lack of slides wasn't a problem, I enjoyed it

Comments are closed