MVVM Light Toolkit
New release: MVVM Light Toolkit V2
Current version: V2.0.0, released 4th of October 2009New alpha release: MVVM Light Toolkit V3 Alpha 3
Includes support for WPF4 and SL4 in Studio 2010 and Blend Preview 4
Current alpha version: V3.0.0/a3, released 30th of November 2009Introduction
The main purpose of the toolkit is to accelerate the creation and development of MVVM applications in WPF and Silverlight.
Like other MVVM implementations, the toolkit helps you to separate your View from your Model which creates applications that are cleaner and easier to maintain and extend. It also creates testable applications and allows you to have a much thinner user interface layer (which is more difficult to test automatically).
This toolkit puts a special emphasis on the "blendability" of the created application (i.e. the ability to open and edit the user interface into Expression Blend), including the creation of design-time data to enable the Blend users to "see something" when they work with data controls.
Much has been written about MVVM as a pattern. I recommend starting by reading Josh Smith's article at MSDN. Also great, Shawn Wildermuth's MSDN article about MVVM applied to Silverlight.
Installation and Creation
The MVVM Light Toolkit installation procedure is described here. You can also install the latest stable version, or the latest alpha/beta version manually by following the indications on this page.
To create a new MVVM Light application, check this article (for Visual Studio) and this one (for Expression Blend).
Components
This toolkit gathers a few helper components for Windows Presentation Foundation and Silverlight:
- The GalaSoft.MvvmLight.dll library with helper classes:
- A ViewModelBase class to be used as the base class for ViewModels.
- A Messenger class (and diverse message types) to be used to communicate within the application. Recipients only receive the message types that they register for. Additionally, a target type can be specified, in which case the message will only be transmitted if the recipient's type matches the target parameter.
Messages can be anything from simple values to complex objects. You can also use provided specialized message types, or create your own types deriving from them.
More information about the Messenger class. - MessageBase: A simple message class, carrying optional information about the message's sender.
- GenericMessage<T>: A simple message with a Content property of type T.
- CommandMessage: Used to send a command (as a string) to a recipient. For example, save your commands as constant in a Commands class, and then send Commands.Save to a recipient.
- CommandMessage<T>: Same as above, but with a generic Content property. Can be used to pass a parameter to the recipient together with the command.
- DialogMessage: Used to request that a recipient (typically a View) displays a dialog, and passes the result back to the caller (using a callback). The recipient can choose how to display the dialog, either with a standard MessageBox, with a custom popup, etc…
- PropertyChangedMessage<T>: Used to broadcast that a property changed in the sender. Fulfills the same purpose than the PropertyChanged event, but in a less tight way.
- Command classes optimized for WPF and Silverlight and simplifying commanding in your application. Available with or without generic parameter (RelayCommand<T> and RelayCommand). For an in-depth analysis, I encourage you to read Using RelayCommands in Silverlight and WPF
For Silverlight, choose File, New Project, Silverlight, Mvvm, MvvmLight.
- Create a new ViewModel.
- Create a new View.
- Create a new ViewModelLocator, a class that holds and manages references to ViewModels.
- mvvminpc adds a new bindable property to a ViewModel.
- mvvmlocatorproperty adds a new ViewModel to a ViewModeLocator.
- mvvmpropa adds a new attached property to a DependencyObject (WPF only).
- mvvmpropdp adds a new dependency property to a DependencyObject (WPF only).
- mvvmslpropa adds a new attached property to a DependencyObject (Silverlight only).
- mvvmslpropdp adds a new dependency property to a DependencyObject (Silverlight only).
Source code and Codeplex
The source code for the GalaSoft.MvvmLight helper library can be downloaded here.
Note: The solution contains the WPF project, the WPF Unit Test project, the Silverlight 3 project and the Silverlight 3 Unit Test project.
I also packaged a version of the source code without unit tests, for people using versions of Visual Studio that do not support this feature.
Finally, if you are a developer, check the Codeplex site for the MVVM Light Toolkit. This is where the latest code is always available, as well as a good place to post suggestions/remarks/questions/discussions about the toolkit.
Articles about the MVVM "light" toolkit
By Laurent
New Versions:
-
MVVM Light V3/alpha3 for Blend 3 and Blend Preview 4
Announces the release of V3 Alpha 3 templates for Blend 3 and Blend Preview 4. Includes Silverlight 4 and WPF4 in Blend Preview 4! -
MVVM Light Toolkit V3 Alpha 3 for WPF4/SL4
Announces the release of V3 Alpha 3 templates for Visual Studio 2008 and 2010. Includes Silverlight 4 and WPF4 in VS2010! -
MVVM Light Toolkit V3 Alpha 2: EventToCommand behavior
Announces the release of V3 Alpha 2, an early preview version with the new EventToCommand behavior, allowing you to bind any event to any ICommand in the ViewModel. Download and use with care!
See also this post for a bug correction and a new feature! -
MVVM Light Toolkit V3 Alpha 1
Announces the release of V3 Alpha 1, an early preview version. Download and use with care! -
MVVM Light Toolkit V2: What’s new?
Announces the release of V2 and lists what is new in this version.
Installation, Creation
- Installing the MVVM Light Toolkit Manually
Describes the manual installation of the MVVM Light Toolkit for WPF and Silverlight, including the latest alpha/beta versions. - Installing the MVVM Light Toolkit
Describes the installation of the MVVM Light Toolkit for WPF and Silverlight. - Creating a new MVVM Light application in Visual Studio
Describes the creation of am MVVM Light application for WPF and Silverlight in Visual Studio 2008. - Creating a new MVVM Light application
in Expression Blend
Describes the creation of am MVVM Light application for WPF and Silverlight in Expression Blend 3. - Understanding the MVVM Light application sample
Explains the various parts of the sample application that is created when a new MVVM Light application is created in Visual Studio.
Documentation about specific components:
-
Silverlight 4: Drag&drop with EventToCommand
Demoes how to use the new PassEventArgsToCommand property in the EventToCommand behavior to implement drag&drop operations in Silverlight 4 without code behind. Get the files directly in the ViewModel! -
MVVM Light Toolkit V3 Alpha 2: EventToCommand behavior
Announces the release of V3 Alpha 2, an early preview version with the new EventToCommand behavior, allowing you to bind any event to any ICommand in the ViewModel. Download and use with care!
See also this post for a bug correction and a new feature! - Clean shutdown in Silverlight and WPF applications
Using the MVVM Light Toolkit's Messenger to create a clean and easily extensible shutdown sequencein WPF and Silverlight applications. - Using RelayCommands in Silverlight and WPF
An in-depth analysis of RelayCommand and RelayCommand<T> -
MVVM Light Toolkit Messenger V2
Announcing the new Messenger component of the MVVM Light Toolkit, with a new simplified API.
By others
- Silverlight 4 Beta, Drop Target et MVVM: (en francais, par Laurent Kempé) Utilise EventToCommand pour passer un fichier au ViewModel via the new Drap&Drop functionality in Silverlight 4.
- MVVM Light Toolkit V2: (in English, by Laurent Kempé) Innoveo Solution is a Swiss firm using the MVVM Light Toolkit in the application that they are developing (cross posted here).
- M-V-VM: ViewModels Intercommunication (in Italian, by Corrado Cavalli)
- WPF and MVVM tutorial 08, messaging (in English, by Raffaele Garofalo) (who talks about the Galactic toolkit, and I think it would have been a cool name ;))
- Série d'articles sur l'approche MVVM pour WPF/Silverlight (in French, by Richard Clark)
- Etude de cas : application Silverlight avec Twitter et MVVM Light (in French, by Richard Clark)
- Laurent Bugnion’s MVVM light toolkit for Silverlight and WPF (in French, by Tweened)
- MVVM light Toolkit für WPF und Silverlight (in German, forum thread with various authors).
Credits
The creation of this toolkit would not have been possible without the following people:
Josh Smith created the RelayCommand and gracefully allowed me to integrate it with very minor changes inside the toolkit and to distribute it. He is also my "go to guy" when I have issues (with my code, I mean).
Marlon Grech started mentioning using a mediator pattern to communicate between ViewModels. This discussion led me to create the Messenger class.
Jaime Rodriguez had numerous discussions with me regarding the creation of ViewModels and gave me food for thoughts...
Glenn Block helped me finalize some thoughts and sparked a lot of new ones. He also gave me great motivation to write this toolkit, and is an early tester and reviewer of the toolkit.
Corrado Cavalli gave me a lot of fantastic feedback and ideas for new features or improving existing features, and was one of the very active early testers.
Laurent Kempé was often here to keep me company in the wee morning hours, to talk about the direction and features of the toolkit, and gave me very valuable feeback. His team at Innoveo is using the MVVM Light Toolkit in their new WPF application, and very supportive when it comes to improving and testing the latest versions!
Brian Henderson and Rob Zelt helped test early versions and gave precious feedback and suggestions to improve the features.
Steven Robbins helped me with an issue and suggested I use a hack to solve it. "I think you can safely say the worse line of code in your toolkit is my one :-)"
All the WPF Disciples for the MANY discussions we had around the pattern and the best way to implement it in various situations. You guys all rock my world!
Praises about the MVVM light toolkit
- @LBugnion is taking out some of the monotony of implementing MVVM http://digg.com/u15wEj with MVVM light toolkit, the monotony killer :-) - @gblock
- Just got the MvvmLight toolkit, looks great, good work! - @CSkardon
- just want to say I love mvvm lite. I love the mediator pattern you have. I use it evrywhere to decouple b/n view & vm interaction - @CoreyGaudin
- The Mvvm Light toolkit v3 alpha 2 by @LBugnion is very good. - @gulrog
- Agreed...your EventToCommand code is very, very useful. Already plugged it in and stripped out the old way I was doing it. - @DanWahlin
- Tweaking my RIA/MVVM app, I think I have fallen in love with MVVM Light's Messenger, a little too much... - @ShawnWildermuth




