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 (3) -

  • 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

Add comment

Loading