This is the default ViewModelLocator
class created by nugetting the mvvmlight
package.
public class ViewModelLocator
{
public ViewModelLocator()
{
ServiceLocator.SetLocatorProvider(() => SimpleIoc.Default);
////if (ViewModelBase.IsInDesignModeStatic)
////{
//// // Create design time view services and models
//// SimpleIoc.Default.Register<IDataService, DesignDataService>();
////}
////else
////{
//// // Create run time view services and models
//// SimpleIoc.Default.Register<IDataService, DataService>();
////}
SimpleIoc.Default.Register<MainViewModel>();
}
public MainViewModel Main
{
get
{
return ServiceLocator.Current.GetInstance<MainViewModel>();
}
}
public static void Cleanup()
{
// TODO Clear the ViewModels
}
}
There is no magic creating any viewmodel inside that class unless you put it in there. By default there is a ServiceLocator
involved, which you can remove and replace by any DI container of your choice or do the instantiation of the ViewModels handled by this ViewModelLocator
manually.
The static Cleanup()
is not called by default and you have to place the call whereever you like to get it called whenever you like. That is all up to you if that is needed at all.
Conclusion: It is all up to you how the ViewModelLocator
will operate and behave
To answer your questions:
- Does that mean that on load it will load all ViewModels in the program?
Nope, unless you write code that will do so
- If it is the default to load all, can I change it to load/instantiate the ViewModels only when the dialog is opened?
(see above)