I have a ViewModelLocator class that I am defining in app.xaml which is used by my views to databind to the correct ViewModel.
DataContext="{Binding HomeViewModel, Source={StaticResource Locator}}"
I am using Prism and Unity and my ViewModelLocator class needs a reference to an application level unity container.
I wanted inject the IUnityContainer into the ctor of the ViewModelLocator but the ViewModelLocator gets instanciated from app.xaml using a parameterless ctor.
Is there a preferred way to get access to an application level container - for all other classes in the app I just use ctor injection to get the global container.
What I am currenlty doing for the ViewModelLocator is defining a static variable in my BootStrapper class to store the container. I create the container by overriding the CreateContainer method on UnityBootStrapper.
protected override IUnityContainer CreateContainer()
{
BootStrapper.DIContainer = base.CreateContainer();
return BootStrapper.DIContainer;
}
Then in the ViewModelLocator class I just reference the BootStrapper.DIContainer property to register my viewmodels
BootStrapper.DIContainer.RegisterType<IShellViewModel, DesignShellViewModel>();
This works fine but it is the only place in the application that needs to reference this static property on the bootstrapper - and would like to get rid of it if possible.
thanks Michael
I had the same issue while converting a Silverlight RIA Business app of mine to use Prism, Unity, and MVVM light toolkit. I came up with this workaround which is to just let the App.xaml create an instance of my ViewModelLocator class and during the application startup event I remove that instance it created from the application resources and re-add an instance using Unity container's Resolve method.
Boostrapper.cs: (UnityBootstrapper class)
ViewModelLocator.cs: (used for blendability)
}
App.xaml.cs:
Your now cocked, locked, and ready to rock..