Getting started with Prism 7.0

For this post I will be using the following:

  • Windows 10 Pro, Version 1709, Build 16299.248
  • Visual Studio Enterprise 2017, Version 15.5.7
  • .NET Framework 4.7.02556
  • Prism 7.0.0.396

When creating WPF applications Prism is a great way to build loosely coupled, maintainable and testable applications. However, the documentation is focused on version 6.3.  If you are curious of how to use the latest version (7.0.0.396 as of this writing) you have to figure out how to initialize your application. This post shows you how to get a new project loading the shell.

Start by creating a new WPF application.

image 

Next we are going to add Prism to our project. Right-click on the project in solution explorer and select Manage NuGet Packages…

image

From the Browse tab make sure Include prelease is checked and search for “Prism.Wpf”.

image

Install Prism.Wpf.

Now repeat the search for “Prism.Unity” and install it.

image

Clear the search box, click the Updates tab and install any updates.

image

Previous versions of Prism used a bootstrapper class. However, 7.0 added a new application base class to use instead.

Open App.xaml and replace the StartupUri attribute with:

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

Now add “prism:Prism” to all the Application elements.

<prism:PrismApplication x:Class="WpfApp1.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"              xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"              xmlns:local="clr-namespace:WpfApp1"
xmlns:prism="http://prismlibrary.com/">
<prism:PrismApplication.Resources>
    </prism:PrismApplication.Resources> </prism:PrismApplication>

Open App.xaml.cs and change the base class from Application to PrismApplication. Then use the light blub to add the using statements.

image

Finally copy and paste the code below into your class and use the light blub to add any using statements.

protected override void RegisterTypes(IContainerRegistry containerRegistry)
{
}

protected override Window CreateShell()
{
return ServiceLocator.Current.GetInstance<MainWindow>(); }

Now you can press F5 to run your Prism 7.0 application.

image

Comments (11) -

  • Thank you very much! Started with Prism 7 and I was lost.
  • After following your instruction step-by-step, I get the following errors:
    Partial declarations of 'App' must not specify different base classes
    'App.RegistrerTypes(IContainerRegistry)': no suitable method found to override
    'App.CreateShell()': no suitable method found to override
    The name 'ServiceLocator' does not exist in the current context
  • Hi,
    This is a nice way to introduce the more recent incarnation of Prism.
    Do you have any articles on using recent Prism for View Discovery, Dependency Injection and IoC?
  • Thanks,With your explanation it was pretty easy to migrate over to this newer version. The only thing I would recommend to add is to mention where to move registered dependency references that may have been in the boostrapper file of the previous versions.
  • Hi,
    I would like to update from 6.3 to 7.

    I seem to have hit a road block.

    The PrismApplication class, CreateShell expects a return type of Window instead of the previous BootStrapper which wanted a DependencyObject.

    My MainShell is a modified  Telerik RadWindow which itself is a modified System.Windows.Controls.HeaderedContentControl and casting to a Window is not possible.

    Is there a way around this so I can use the PrismApplication object or do I have to roll back and use the BootStrapper like before?
    • Yeah, Telerik did a very strange thing creating a control called RadWindow that is not a Window.  You will return null from CreateShell, and then override InitializeShell to perform your custom logic.  See this code-snippet : github.com/.../1413#issuecomment-394369462
  • Great post! I want to leave a small tip for everyone. First, just add the Prism.Unity.Wpf nuget package, it will bring in all the dependencies (like Prism.Wpf) automatically.  Also, DO NOT update the Unity container packages separately. Prism has references to specific versions of various containers, and if you upgrade that container to a version that Prism does not reference, you will break binary compatibility.  Always use the version of the Unity container that the Prism.Unity.Wpf packages depends on.

Add comment

Loading