Initial steps to setup a Prism application

I haven’t touched Prism in a while as I’ve been using Caliburn.Micro a lot, but decided to reaquaint myself with PRISM recently, so this give me the oppurtunity to create some posts on the basics of PRISM.

Creating the bare bones application

  • Create a new WPF Application
  • Open App.xaml and delete the StartupUri=”MainWindow.xaml” code as we’ll be creating the “shell” window in code
  • Either delete MainWindlow.xaml and then add a new WPF Window or rename the MainWindow to Shell (by convention the main window of the application is named Shell)
  • Add the following code to the XAML, inside the Shell’s grid (just so we have something to view when the application start’s up)
    <TextBlock Text="Shell Application" />
    
  • Using NuGet Install the PRISM package (mine’s version 5.0.0)
  • Create a new class named Bootstrapper, the contents depend upon the IoC container we want to use (discussed below). For now, change the Bootstrapper code to look like this, which is shared by both standard Prism IoC containers.
    public class Bootstrapper
    {
       protected override void InitializeShell()
       {
          base.InitializeShell();
          App.Current.MainWindow = (Window)Shell;
          App.Current.MainWindow.Show();
       }
    
       protected override DependencyObject CreateShell()
       {
          return null;
       }
    }
    

    Obviously we’ve not got a base class at this point so this will not compile, but these two methods are overidden for both Unity and Mef implementation.

  • Finally, for the shared code, open App.xaml.cs and add the following
    protected override void OnStartup(StartupEventArgs e)
    {
       base.OnStartup(e);
    
       Bootstrapper bootstrapper = new Bootstrapper();
       bootstrapper.Run();
    }
    

Okay, at this point we’ve got the basics in place but we need to create the bootstrapper which is used to create the shell via the IoC container, whether that be Unity, Mef or any other container setup to work with PRISM.

Using Unity

The Unity container requires the least work to get up and running.

  • Using NuGet add the package Prism.UnityExtensions (mine’s version 5.0.1) to the solution
  • Change the Bootstrapper code to derive from UnityBoostrapper
  • Change the CreateShell code to look like the following
    protected override DependencyObject CreateShell()
    {
       return Container.TryResolve<Shell>();
    }
    

Using MEF

Using MEF requires a little more work than Unity.

  • Using NuGet add the package Prism.MEFExtensions (mine’s version 5.0.0) to the solution
  • Change the Bootstrapper code to derive from MefBoostrapper
  • Add a reference to System.ComponentModel.Composition
  • Change the CreateShell code to look like the following
    protected override DependencyObject CreateShell()
    {
       return Container.GetExportedValue<Shell>();
    }
    
  • We now need to add our assembly to the MEF catalog, so add the following to the Bootstrapper class
    protected override void ConfigureAggregateCatalog()
    {
       base.ConfigureAggregateCatalog();
       AggregateCatalog.Catalogs.Add(new AssemblyCatalog(GetType().Assembly));
    }
    
  • Finally, we need to mark to Shell with the ExportAttribute so that MEF can locate it via Container.GetExportedValue. So open Shell.xaml.cs and place Export able the class thus
    [Export]
    public partial class Shell : Window
    {
        // code
    }