What’s new in MVVM Light V3

.NET, Blend, MIX, MVVM, Silverlight, Technical stuff, Work, WPF
See comments

V3 of the MVVM Light Toolkit was released during MIX10, after quite a long alpha stage. This post lists the new features in MVVM Light V3.


MVVM Light Toolkit V3 can be installed for the following tools and framework versions:

  • Visual Studio 2008 SP1, Expression Blend 3
    • Windows Presentation Foundation 3.5 SP1
    • Silverlight 3
  • Visual Studio 2010 RC, Expression Blend 4 beta
    • Windows Presentation Foundation 3.5 SP1
    • Windows Presentation Foundation 4 RC
    • Silverlight 3
    • Silverlight 4 RC

For more information about installing the MVVM Light Toolkit V3, please visit this page. For cleaning up existing installation, see this page.

New in V3 RTM

The following features have been added after V3 alpha3:

Project template for the Windows Phone 7 series (Silverlight)

This new template allows you to create a new MVVM Light application in Visual Studio 2010 RC and to run it in the Windows Phone 7 series emulator. This template uses the Silverlight 3 version of the MVVM Light Toolkit V3. At this time, only the essentials features of the GalaSoft.MvvmLight.dll assembly are supported on the phone.

New in V3 alpha3

The following features have been added after V3 alpha2:

New logo

An awesome logo has been designed for MVVM Light by Philippe Schutz.

DispatcherHelper class (in GalaSoft.MvvmLight.Extras.dll)

This class is useful when you work on multi-threaded WPF or Silverlight applications.

Initializing: The DispatcherHelper class must be initialized in the UI thread. For example, you can initialize the class in a Silverlight application’s Application_Startup event handler, or in the WPF application’s static App constructor (in App.xaml).

// Initializing in Silverlight (in App.xaml)

private void Application_Startup(
    object sender,
    StartupEventArgs e)
    RootVisual = new MainPage();

// Initializing in WPF (in App.xaml)

static App()

Verifying if a property exists

The ViewModelBase.RaisePropertyChanged method now checks if a given property name exists on the ViewModel class, and throws an exception if that property cannot be found. This is useful to detect typos in a property name, for example during a refactoring. Note that the check is only done in DEBUG mode.

Replacing IDisposable with ICleanup

The IDisposable implementation in the ViewModelBase class has been marked obsolete. Instead, the ICleanup interface (and its Cleanup method) has been added. Implementing IDisposable in a ViewModel is still possible, but must be done explicitly. IDisposable in ViewModelBase was a bad practice, because it supposes that the ViewModel is garbage collected after Dispose is called. instead, the Cleanup method does not have such expectation.

The ViewModelLocator class (created when an MVVM Light project template is used in Visual Studio or Expression Blend) exposes a static Cleanup method, which should in turn call each ViewModel’s Cleanup method. The ViewModel is free to override the Cleanup method if local cleanup must be performed.

Passing EventArgs to command with EventToCommand

The EventToCommand class is used to bind any event to an ICommand (typically on the ViewModel). In this case, it can be useful to pass the event’s EventArgs parameter to the command in the ViewModel. For example, for the MouseEnter event, you can pass the MouseEventArgs to a RelayCommand<MouseEventArgs> as shown in the next listings.

Note: Bringing UI specific classes (such as EventArgs) into the ViewModel reduces the testability of the ViewModel, and thus should be used with care.

Setting EventToCommand and PassEventArgsToCommand:

<Grid x:Name="LayoutRoot">
        <i:EventTrigger EventName="MouseEnter">
            <cmd:EventToCommand Command="{Binding MyCommand}"
                                PassEventArgsToCommand="True" />

Getting the EventArgs in the command

public RelayCommand<MouseEventArgs> MyCommand
    private set;

public MainViewModel()
    MyCommand = new RelayCommand<MouseEventArgs>(e =>
        // e is of type MouseEventArgs

Changes to templates

Various changes have been made to project templates and item templates to make them more compatible with Silverlight 4 and to improve their visibility in Visual Studio and Expression Blend.

Bug corrections

  • When a message is sent through the Messenger class using the method Messenger.Default.Send<T>(T message, object token), and the token is a simple value (for example int), the message was not sent correctly. This bug is now corrected.

New in V3

The following features have been added after V2.

Sending messages with callback

Certain classes have been added to the GalaSoft.MvvmLight.Messaging namespace, allowing sending a message and getting a callback from the recipient. These classes are:

  • NotificationMessageWithCallback: Base class for messages with callback.
  • NotificationMessageAction: A class with string notification, and a parameterless callback.
  • NotificationMessageAction<T>: A class with string notification, and a callback with a parameter of type T.

To send a message with callback, use the following code:

var message = new NotificationMessageAction<bool>(
    "Hello world",
    callbackMessage =>
        // This is the callback code
        if (callbackMessage)
            // ...


To register and receive a message with callback, use the following code:

    message =>
        // Do something
        // Execute the callback

Messenger.Default can be overriden

The Messenger.Default property can also be replaced, for example for unit testing purposes, by using the Messenger.OverrideDefault method. All the public methods of the Messenger class have been made virtual, and can be overridden in the test messenger class.

Sending messages to interfaces

In V2, it was possible to deliver messages targeted to instances of a given class. in V3 it is still possible, but in addition you can deliver a message to instances that implement a certain interface. The message will not be delivered to other recipients.

Use the overload Messenger.Default.Send<TMessage, TTarget>(TMessage message) where TTarget is, in fact, an interface (for example IDisposable). Of course the recipient must register to receive the type of message TMessage.

Sending messages with a token

Messages can now be sent through the Messenger with a token.

  • To send a message with token, use the method overload Send<TMessage>(TMessage message, object token).
  • To receive a message with token, use the methods Register<TMessage>(object recipient, object token, Action<TMessage> action) or Register<TMessage>(object recipient, object token, bool receiveDerivedMessagesToo, Action<TMessage> action)

The token can be a simple value (int, string, etc…) or an instance of a class. The message is not delivered to recipients who registered with a different token, or with no token at all.

Renaming CommandMessage to NotificationMessage

To avoid confusion with ICommand and RelayCommand, the CommandMessage class has been renamed to NotificationMessage. This message class can be used to deliver a notification (of type string) to a recipient.

ViewModelBase constructor with IMessenger

The ViewModelBase class now accepts an IMessenger parameter. If this constructor is used instead of the default empty constructor, the IMessenger passed as parameter will be used to broadcast a PropertyChangedMessage when the method RaisePropertyChanged<T>(string propertyName, T oldValue, T newValue, bool broadcast) is used.

In the default ViewModelBase constructor is used, the Messenger.Default instance will be used instead.

EventToCommand behavior

The EventToCommand behavior has been added in V3. It can be used to bind any event of any FrameworkElement to any ICommand (for example a RelayCommand located in the ViewModel). More information about the EventToCommand behavior can be found here and here.

Updated the project templates to remove the sample application

The project template has been updated to remove the sample application that was created every time that a new MVVM Light application was created in Visual Studio or Blend. This makes the creation of a new application easier, because you don’t need to remove code before you can start writing code.

Bug corrections

Some bugs that were in Version 2 have been corrected:

  • In some occasions, an exception could be thrown when a recipient was registered for a message at the same time as a message was received.

New names for DLLs

If you upgrade an existing installation, you will need to change the reference to the DLLs in C:\Program Files\Laurent Bugnion (GalaSoft)\Mvvm Light Toolkit\Binaries. The assemblies have been moved, and the versions for Silverlight 4 and for WPF4 have been renamed, to avoid some confusion. It is now easier to make sure that you are using the correct DLL.

WPF3.5SP1, Silverlight 3

When using the DLLs, make sure that you use the correct versions.

WPF4, Silverlight 4

When using the DLLs, make sure that you use the correct versions.

Previous entry | Next blog entry

Responses to “What’s new in MVVM Light V3”

  1. Leo Says:

    Hi I have an issue, I am calling a wpf window, using window.show from a menuitemcommand on a visual studio package. When I close the window, using window.close(), the window closes, but if I tried to call to open the window again frm the menuitemcommand, it opens the last window closed, not a new one. How can I dispose the whole window and resources to everytime that I call the window.show() opens a new window?


  2. lbugnion Says:


    Without seeing the code, impossible to say, but I am ready to bet that you are keeping the new window as a private attribute. Just discard it and you should be good.


Comments for What&#8217;s new in MVVM Light V3