This project is read-only.


Please install using NuGet:

To install the library, run the following command in the Package Manager Console

PM> Install-Package Prism.Unity.Bootstrapper

Using the Library

This library assumes you are already using the Prism for Windows Store libraries and your application object currently inherits from MvvmAppBase. Open the code behind in your application for App.xaml and replace MvvmAppBase with MvvmUnityAppBase.

sealed partial class App : MvvmAppBase

sealed partial class App : MvvmUnityAppBase

NOTE: If you have implemented the OnInitialize() method rename it to OnApplicationInitialize().

Using with Prism

This library automatically creates the Unity Container and wires it up in Prism.When you use this library, ViewModels will be retrieved through Unity using the ViewModelLocator.SetDefaultViewModelFactory method in the Prism library. In addition, this library overrides the Resolve method and wires the unity container in for you.

Registering Types in Unity

Standard Registration

In your code behind or App.xaml override the method OnContainerRegistration and add code to register your types.

protected override void OnContainerRegistration(IUnityContainer container)
	// ***
	// *** Register types in the IoC Container
	// ***

	// ***
	// *** Add Services
	// ***
	container.RegisterType<IDispatcherService, DispatcherService>(new ContainerControlledLifetimeManager());
	container.RegisterType<ITemplateManager, TemplateManager>(new ContainerControlledLifetimeManager());
	container.RegisterType<IApplicationSettingsRepository, ApplicationSettingsRepository>(new ContainerControlledLifetimeManager());
	container.RegisterType<ITemplateDefaultsRepository, ApplicationSettingsRepository>(new ContainerControlledLifetimeManager());
	container.RegisterType<ILabelPrintService, LabelPrintService>(new ContainerControlledLifetimeManager());

	// ***
	// *** Add Unity and Prism objects
	// ***
	container.RegisterType<IAudioItemRepository, AudioItemRepository>(new ContainerControlledLifetimeManager());

	// ***
	// *** Register ViewModels as Singletons
	// ***
	container.RegisterType<AudioPageViewModel, AudioPageViewModel>(new ContainerControlledLifetimeManager());
	container.RegisterType<PrinterOptionsViewModel, PrinterOptionsViewModel>(new ContainerControlledLifetimeManager());
	container.RegisterType<PreviewPageViewModel, PreviewPageViewModel>(new ContainerControlledLifetimeManager());

Early Registration

The standard registration method is called during the OnInitialize() method of the Application class. At the time this method is called the UI thread is available and the session and navigation services are ready. However, if the application is being restored from state, this process has already been completed. If you need to register types that are necessary for restoring state you can override the OnEarlyContainerRegistration() method. Note that some types should not be initialized here such as the Pub/Sub events when they are required on the UI thread.

Accessing the Unity Container from within your Application

The Unity container can be access via the Service Locator as follows:

IDispatcherService dispatcher = ServiceLocator.Current.GetInstance<IDispatcherService>();

If you have registered the Unity Container (as shown in the registration code sample), you can get an instance of the container as follows:

IUnityContainer unityContainer = ServiceLocator.Current.GetInstance<IUnityContainer>();

There is also a property on the App object named Container that provides direct access to the Unity Container. Using the app base object's Current property, you can access the container anywhere using

var mediaElement = App.Current.Container.Resolve<IMediaElement>();

Last edited Nov 4, 2014 at 12:40 PM by porrey, version 8