How to set up PRISM in a WPF App

WPF PRISM Set Up - PRISM Library Logo

If you’re like me, then part of learning a new library or framework is the process of setting up your environment without all of the fancy tools and templates that are available for it. Of course, that’s also one of the most frustrating aspects of learning the new library as well. So, in this article, I will go over the basic steps of how to set up PRISM in a WPF App with NuGet packages and base code changes. I will also point you to a variety of helpful resources and templates so that you only have to do this once.

First, let’s start off by opening visual studio and creating a new project. At the new project dialog, select the language of your choice on the left hand side. Select “WPF App (.NET Framework)” in the middle. Set a name and a location at the bottom, and click “OK”.

WPF PRISM Set Up - File Menu, New Project
WPF PRISM Set Up - New Project Dialog
PRISM Package Set Up

I called my project PRISM-WPF-SETUP. Once the project is created, you will automatically be placed in the MainWindow designer. However, the first thing we must do is set our NuGet packages. Right-click on the project in the Solution Explorer window. Select the “Manage NuGet Packages”. This will open the NuGet Manager, a visual way to install packages for your project.

WPF PRISM set up - Solution Explorer Context Menu
WPF PRISM set up - NuGet Package Manager

Now, we are going to search for and install three NuGet packages. Those packages are:

These three packages are the only three you need to set up PRISM in a WPF App. Alternatively, you could directly install the packages using the Package Management window by running the following commands:

  • Install-Package PRISM.Core
  • Install-Package PRISM.WPF
  • Install-Package PRISM.Unity
Modify the App.xaml for PRISM

In order for PRISM to function properly, there exists a concept called the “shell”. This is a common way of saying main window, since PRISM operates across a variety of platforms that may not have a main window. In WPF Apps, the primary window and the shell are the same thing. However, we must align our startup routines to jive with PRISM’s needs as well. So, our next step is to make a set of changes to the App.xaml file.

First, locate the App.xaml in the solution explorer. Double-click to show the designer screen. The designer screen is generally split into two panes. One for the visual, and another for the XAML code. In the XAML code section, you will some XAML that looks very similar to this:

<Application x:Class="PRISM_WPF_SETUP.App"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:local="clr-namespace:PRISM_WPF_SETUP"
             StartupUri="MainWindow.xaml">
    <Application.Resources>
         
    </Application.Resources>
</Application>

We need to make some simply changes to this code file. We’ll start by adding an XML namespace just below the “local” namespace. The new namespace we need is

xmlns:prism="http://prismlibrary.com"

When this namespace has been added, we can now have the Application inherit from the base prism application object instead of the default WPF project. One thing that’s important to note is that in XAML, the root object is a class. That is why you have the x:Class attribute on the application in the XAML, as it points to the specific implementation in the code. Consequently, for our next step, we need to change the “<Application” text to read “<prism:PrismApplication” . This will automatically change the closing tag. However, the sub-element, Application.Resources does not automatically change, so change that as well to prism:PrismApplication.Resources. Finally, remove the StartupUri attribute and value, because PRISM will handle the window loading procedure.

When those changes have been made, the App.xaml will look like this:

<prism:PrismApplication x:Class="PRISM_WPF_SETUP.App"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:local="clr-namespace:PRISM_WPF_SETUP"
             xmlns:prism="http://prismlibrary.com/">
    <prism:PrismApplication.Resources>
         
    </prism:PrismApplication.Resources>
</prism:PrismApplication>
Update the App.xaml.cs code behind

In this section, we’ll change the code behind of the App.xaml. Due to the changes we made in the App.xaml, the code behind needs to be updated to reflect the appropriate base class from PRISM. The initial code behind generated when the project was created should look pretty similar to this:

using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Threading.Tasks;
using System.Windows;

namespace PRISM_WPF_SETUP
{
    /// <summary>
    /// Interaction logic for App.xaml
    /// </summary>
    public partial class App : Application
    {
    }
}

First, add a using statement for Prism.Unity to the top of the file. Second, change the base class from which App inherits to be PrismApplication instead of Application. This ensures that both the XAML and its code behind agree on a base class. Next, build¬†the¬†project. This is important. Building the project regenerates code from the XAML declarations which will allow you to perform the next steps with intellisense. You will get a couple of errors. There will be a red squiggle under the App class name, hover over that and select “Implement Abstract Class”. This will add the two methods you need to have in this class. Your code will now look like this:

using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Threading.Tasks;
using System.Windows;
using Prism.Ioc;
using Prism.Unity;

namespace PRISM_WPF_SETUP
{
    /// <summary>
    /// Interaction logic for App.xaml
    /// </summary>
    public partial class App : PrismApplication
    {
        protected override Window CreateShell()
        {
            throw new NotImplementedException();
        }

        protected override void RegisterTypes(IContainerRegistry containerRegistry)
        {
            throw new NotImplementedException();
        }
    }
}

We’re almost done with this class. For now, delete the throw new NotImplementedException lines from both methods. You will be leaving the RegisterTypes method empty for the time being. In the CreateShell method, add two lines of code. The first line is going to resolve our main window for the project from the Unity container on the PrismApplication base class. So, declare a variable of the MainWindow type called shell and set it equal to Container.Resolve<MainWindow>(), as shown below.

MainWindow shell = Container.Resolve<MainWindow>();

Next, simply return the shell variable from this method.

return shell;

When you’ve added those two lines build and run the project. Assuming you have a successful build, then the main window should display when the App is run. If you get two main windows, then you didn’t remove the StartupUri from App.xaml. The resulting code in App.xaml.cs will look like this:

using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Threading.Tasks;
using System.Windows;
using Prism.Ioc;
using Prism.Unity;

namespace PRISM_WPF_SETUP
{
    /// <summary>
    /// Interaction logic for App.xaml
    /// </summary>
    public partial class App : PrismApplication
    {
        protected override Window CreateShell()
        {
            MainWindow shell = Container.Resolve<MainWindow>();

            return shell;
        }

        protected override void RegisterTypes(IContainerRegistry containerRegistry)
        {
            
        }
    }
}
Conclusion

It’s a good exercise to be able to manually set up PRISM in a WPF app. However, like most things in this world, just because you can doesn’t mean you should. There is a wonderful set of templates available here for developers who frequently work on PRISM projects. These templates don’t just include initial project templates, but also item templates to ease the process of adding Views & View Models and Snippets which allow you to use shorter key words to generate properties, commands, and more.

In conclusion, I encourage anyone who found this walk-through helpful to also spend some time on the PRISM Website, review the documentation and samples there, and see how much a library such as this can organize and speed development of Desktop, Web, and Mobile application.

We want to help with your Software Development PaynePoints!

Reach out and get experience development help from an experienced developer
Posted in Articles, Software and tagged , , , , , , , , .

Leave a Reply

Your email address will not be published. Required fields are marked *

five − four =