Click here to Skip to main content
13,898,662 members
Click here to Skip to main content
Add your own
alternative version

Tagged as


7 bookmarked
Posted 18 Sep 2012
Licenced CPOL

SimpleIoc and the Messenger Service in MVVM Light

, 18 Sep 2012
Rate this:
Please Sign up or sign in to vote.
SimpleIoc and the Messenger Service in MVVM Light.

The SimpleIoc addition in the most recent version of MVVMLight is a great add and really simplifies the ViewModelLocator across all of the platforms supported in this framework.

If you are not familiar with MVVM Light or what types of project platforms it supports please go to or to learn more.

However if you are using the Messenger Service in MVVM Light to perform a set of actions to do the following:

  • User takes an action
  • Application changes views
  • Application send message to display or load data

Then it is important to note that there are some differences in the way that the ViewModels are loaded in comparison to the previous version of MVVM Light.

Let's look at the ViewModelLocator in the new version:

public class ViewModelLocator
    static ViewModelLocator()
        ServiceLocator.SetLocatorProvider(() => SimpleIoc.Default);

        if (ViewModelBase.IsInDesignModeStatic)
            SimpleIoc.Default.Register<IDataService, Design.DesignDataService>();
            SimpleIoc.Default.Register<IDataService, DataService>();

    /// <summary>
    /// Gets the Main property.
    /// </summary>
    Justification = "This non-static member is needed for data binding purposes.")]
    public MainViewModel Main
            return ServiceLocator.Current.GetInstance<MainViewModel>();

Notice that in comparison to what you may have seen in the past version, there is a single property to the ViewModel and a simple line in the constructor here to register the ViewModel with the Ioc Container and that is it.

The property for the ViewModel is Lazy, meaning that it is loaded on demand and is a singleton.  You can create multiple instances by passing in a key upon creation, but for this example I'll keep it simple.

Given the scenario above you will get the result of the secondary view not getting the message sent event though you have registered the message with the Message Service and have an action ready to handle it.  BUT the second time through it's fine and operates as it should.  What has happened?

Very simply, the examples you have seen in the past very rarely ever cover situations in the real world nor do they ever go far enough to look at the overloads in the constructor etc.

What has happened here is that the ViewModel must exist first in order to take action on the message. Moreover, the Messenger Service in MVVM Light does not operate like a Service Bus or Message Queue where the messages stay there until they are read.  It is more similar to Events, a fire and forget model.

In order to alleviate said problem, simple pass in "true" to the Register call in the SimpleIoc to create the instance of the ViewModel immediately and go on with your coding.


The previous version of MVVM Light created all of the ViewModels immediately, although not too much of an issue; there are instances where a user may not go to every view in the application and the idea here is to only load what is needed and keep the View and ViewModels loaded in memory when necessary.

If you have questions please comment and/or contact me on twitter @spboyer.


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Shayne P Boyer
United States United States
I work on Azure, ASP.NET Core content and Open Source, speak at national and community events while helping teams architect web and cloud applications.

You may also be interested in...


Comments and Discussions

BugNeed to update Pin
Clifford Nelson1-Feb-17 8:00
mvaClifford Nelson1-Feb-17 8:00 
QuestionYou solved my problem Pin
Medo_Omar30-Aug-14 2:25
memberMedo_Omar30-Aug-14 2:25 
GeneralMy vote of 3 Pin
rajesh-kb2-Oct-13 6:48
memberrajesh-kb2-Oct-13 6:48 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Cookies | Terms of Use | Mobile
Web06 | 2.8.190306.1 | Last Updated 18 Sep 2012
Article Copyright 2012 by Shayne P Boyer
Everything else Copyright © CodeProject, 1999-2019
Layout: fixed | fluid