1. Windows Store apps made easy with Prism and Prism project templates
2. Views, ViewModels and navigation
3. Configuring Prism using Unity or Ninject

Manual registration of ViewModels with ViewModelLocator is dull and error prone – you can always forget to register new class or copy-paste incorrect definition.

Fortunately, the configuration process can be automated by using dependency injection container, for example Unity or Ninject.

Setting up Unity

When you are starting a new app, you have an option to create a fully configured Unity-based Prism app. My demo was created using the blank Prism app template and I am going to modify the code to introduce Unity.

First, use NuGet to add Unity to the project, create an instance of UnityContainer in App class.

NuGet Unity


private readonly IUnityContainer _container = new UnityContainer();

Than you can modify OnInitialize() method to register NavigationService in Unity container and override Resolve() function to link Unity with Prism framework.


using Microsoft.Practices.Prism.StoreApps;
using Microsoft.Practices.Unity;
using System;
using Windows.ApplicationModel.Activation;

namespace PrismUnityApp
{
    sealed partial class App : MvvmAppBase
    {
        private readonly IUnityContainer _container = new UnityContainer();

        public App()
        {
            this.InitializeComponent();
        }

        protected override void OnLaunchApplication(LaunchActivatedEventArgs args)
        {
            NavigationService.Navigate("Main", null);
        }

        protected override void OnInitialize(IActivatedEventArgs args)
        {
            _container.RegisterInstance(NavigationService);
        }

        protected override object Resolve(Type type)
        {
            return _container.Resolve(type);
        }
    }
}

That is it. From now on Unity will take care about ViewModels resolution and creation. New pages will not require configuration changes.

Setting up Ninject

To use Prism with Ninject you need the same steps as in case of Unity – add Ninject package, register NavigationService and link container with Prism type resolver.

NuGet Ninject


using Microsoft.Practices.Prism.StoreApps;
using Microsoft.Practices.Prism.StoreApps.Interfaces;
using Ninject;
using Windows.ApplicationModel.Activation;

namespace PrismNinjectApp
{
    sealed partial class App : MvvmAppBase
    {
        private IKernel _ninjectKernel = new StandardKernel(); 

        public App()
        {
            this.InitializeComponent();
        }

        protected override void OnLaunchApplication(LaunchActivatedEventArgs args)
        {
            NavigationService.Navigate("Main", null);
        }

        protected override void OnInitialize(IActivatedEventArgs args)
        {
            _ninjectKernel.Bind().ToConstant(NavigationService);
        }

        protected override object Resolve(System.Type type)
        {
            return _ninjectKernel.Get(type);
        }
    }
}

blog comments powered by Disqus