Dependency registration in SimpleIoc, by default uses Lazy Instantiation, meaning the object won’t be created until it is requested, in this case, via the property on the ViewModelLocator class.
Due to configuring subscription of a message type in the Constructors of my ViewModels in MVVM Light I recently came across a situation whereby messages were being published prior to them having been subscribed to, meaning that the messages disappeared into the void unhandled. The ViewModels relating to the pages I had navigated to were available in the ServiceLocator, but the pages that I hadn’t navigated to were not being instantiated.
I needed to change this behaviour, and handily SimpleIoc provides a way to do this at the point of registration.
Instead of the default :
SimpleIoc.Default.Register<MainViewModel>();
simply use:
SimpleIoc.Default.Register<MainViewModel>(true);
This sets the createInstanceImmediately parameter in the Register method, meaning that the ViewModel is now Eagerly Instantiated and available in the ServiceLocator, or notably in our case, our messages have been subscribed to via the constructors at this point.