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

  • Karel Král

    6/1/2018 2:59:29 PM | Reply

    Thank you very much! Started with Prism 7 and I was lost.

    • Brian Lagunas

      6/1/2019 3:08:18 PM | Reply

      The best way to create a new Prism application is to use the Prism Template Pack on the Visual Studio Marketplace (marketplace.visualstudio.com/items)

      • Jørn Pettersen

        7/31/2019 5:00:05 PM | Reply

        Brian, has the template pack been updated with the new api in v7 for wpf? Also find it hard to find guidelines or documentation for the new api. Saw your YouTube videos and they helped alot. Any plans for an updated pluralsight (or any other plattform) prism for wpf course?

  • Joel Charbonnet

    10/24/2018 10:08:00 PM | Reply

    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

    • Brian Lagunas

      6/1/2019 3:07:46 PM | Reply

      You probably didn't update the App.xaml to derive from PrismApplication too. You can't just update the App.xaml.cs, but you have to update the XAML file too. The best way to create a new Prism application is to use the Prism Template Pack on the Visual Studio Marketplace (marketplace.visualstudio.com/items)

  • Joel Charbonnet

    10/24/2018 10:14:40 PM | Reply

    Done! Problem Solved! Had to add Unity via NuGet.

    • Aphiwe

      12/6/2018 11:56:00 AM | Reply

      Thank you, this worked for me too.

  • Martin

    12/3/2018 4:22:19 PM | Reply

    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?

    • Brian Lagunas

      2/2/2019 3:57:56 PM | Reply

      You should check out the video tutorials that are being created https://www.youtube.com/brianlagunas

      • Stefan

        3/8/2019 3:01:32 PM | Reply

        sure, but there are only a few

        • Brian Lagunas

          6/1/2019 3:09:11 PM | Reply

          New ones are being added. It's a work in progress.

  • Kobie Williams

    12/3/2018 8:39:28 PM | Reply

    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.

  • Lance Benger

    1/23/2019 12:12:25 PM | Reply

    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?

    • Brian Lagunas

      2/2/2019 3:54:31 PM | Reply

      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

  • Brian Lagunas

    2/2/2019 3:52:30 PM | Reply

    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.

    • Rene Rendon

      4/11/2019 12:12:41 PM | Reply

      I'm not seeing a NuGet package under 'Prism.Unity.Wpf'. Am I missing something?

      • Adam Hamilton

        4/29/2019 1:49:55 AM | Reply

        I just used Prism.Unity - this will bring in all the dependencies including Prism.Wpf

  • Tuck

    4/16/2019 1:11:09 PM | Reply

    I get an exception on startup concerning the Unity.Abstraction assembly (using Visual Studio 2017) ... can anyone help ?

    System.IO.FileLoadException  HResult=0x80131040
      Message=Could not load file or assembly 'Unity.Abstractions, Version=3.3.1.0, Culture=neutral, PublicKeyToken=6d32ff45e0ccc69f' or one of its dependencies. The located assembly's manifest definition does not match the assembly reference. (Exception from HRESULT: 0x80131040)
      Source=Prism.Unity.Wpf
      StackTrace:
       at Prism.Unity.PrismApplication.CreateContainerExtension()
       at Prism.PrismApplicationBase.Initialize()
       at Prism.PrismApplicationBase.InitializeInternal()
       at Prism.PrismApplicationBase.OnStartup(StartupEventArgs e)
       at System.Windows.Application.<.ctor>b__1_0(Object unused)
       at System.Windows.Threading.ExceptionWrapper.InternalRealCall(Delegate callback, Object args, Int32 numArgs)
       at System.Windows.Threading.ExceptionWrapper.TryCatchWhen(Object source, Delegate callback, Object args, Int32 numArgs, Delegate catchHandler)
       at System.Windows.Threading.DispatcherOperation.InvokeImpl()
       at System.Windows.Threading.DispatcherOperation.InvokeInSecurityContext(Object state)
       at MS.Internal.CulturePreservingExecutionContext.CallbackWrapper(Object obj)
       at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
       at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
       at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
       at MS.Internal.CulturePreservingExecutionContext.Run(CulturePreservingExecutionContext executionContext, ContextCallback callback, Object state)
       at System.Windows.Threading.DispatcherOperation.Invoke()
       at System.Windows.Threading.Dispatcher.ProcessQueue()
       at System.Windows.Threading.Dispatcher.WndProcHook(IntPtr hwnd, Int32 msg, IntPtr wParam, IntPtr lParam, Boolean& handled)
       at MS.Win32.HwndWrapper.WndProc(IntPtr hwnd, Int32 msg, IntPtr wParam, IntPtr lParam, Boolean& handled)
       at MS.Win32.HwndSubclass.DispatcherCallbackOperation(Object o)
       at System.Windows.Threading.ExceptionWrapper.InternalRealCall(Delegate callback, Object args, Int32 numArgs)
       at System.Windows.Threading.ExceptionWrapper.TryCatchWhen(Object source, Delegate callback, Object args, Int32 numArgs, Delegate catchHandler)
       at System.Windows.Threading.Dispatcher.LegacyInvokeImpl(DispatcherPriority priority, TimeSpan timeout, Delegate method, Object args, Int32 numArgs)
       at MS.Win32.HwndSubclass.SubclassWndProc(IntPtr hwnd, Int32 msg, IntPtr wParam, IntPtr lParam)
       at MS.Win32.UnsafeNativeMethods.DispatchMessage(MSG& msg)
       at System.Windows.Threading.Dispatcher.PushFrameImpl(DispatcherFrame frame)
       at System.Windows.Threading.Dispatcher.PushFrame(DispatcherFrame frame)
       at System.Windows.Application.RunDispatcher(Object ignore)
       at System.Windows.Application.RunInternal(Window window)
       at System.Windows.Application.Run(Window window)
       at System.Windows.Application.Run()
       at PrismWpfApp.App.Main() in C:\Users\tuck\Documents\Visual Studio 2017\Projects\PrismWpfApp\PrismWpfApp\obj\Debug\App.g.cs:line 57

    • Brian Lagunas

      6/1/2019 3:02:50 PM | Reply

      Make sure you do not update any of the container packages separately. The best way to create a new Prism application is to use the Prism Template Pack on the Visual Studio Marketplace (marketplace.visualstudio.com/items)

  • Arulraj

    5/10/2019 1:10:04 PM | Reply

    Can I use MEF with prism? Please give us some examples

    • Brian Lagunas

      6/1/2019 3:04:44 PM | Reply

      Prism no longer supports MEF as a container, but you can still use it as a stand alone solution. Using it would be no different than using it in any other application. The only time I would recommend using MEF is when you have unknown third-parties injecting functionality into your app.

  • syryls

    5/31/2019 8:19:54 PM | Reply

    I tried to install Prism for net core 3 WPF app but it does not work. Visual studio throwing an error:
    "Partial declarations of 'App' must not specify different base classes"
    I'm sure that Prism.wpf and Prism.unity were loaded. Have i forgotten something?

    • Brian Lagunas

      6/1/2019 3:05:43 PM | Reply

      You probably didn't update the App.xaml to derive from PrismApplication too. You can't just update the App.xaml.cs, but you have to update the XAML file too. The best way to create a new Prism application is to use the Prism Template Pack on the Visual Studio Marketplace (marketplace.visualstudio.com/items)

  • Michael Burks

    6/7/2019 6:57:32 PM | Reply

    Hi Brian,
    We have a weird situation where we need to open a Prism application from Progress OpenEdge ABL code by creating an instance of the MainWindow class and calling Show(); It seems that doing it this way causes the bootstrapping process to be bypassed.  Is there a way to open a Prism WPF app (from anywhere, even in a popup window within another xaml application) and have the startup/bootstrapping process run correctly?

  • Michael Burks

    6/12/2019 1:46:37 PM | Reply

    Hi Brian,
    Is it possible to start a Prism application from another application (eg .. open the prism app in a new window) and have the bootstrapping process still happen?  My problem is that we have a hybrid Progress OpenEgde/WPF application and want to use Prism. The .NET dlls are imported into the Progress application (which means that the Prism WPF app has to be a class library instead of an exe).  Is there a way to manually bootstrap the Prism WPF app?

  • Chris Dunn

    8/16/2019 10:03:05 AM | Reply

    Is it possible to use Prism 7 with a UWP application?

  • Vinh

    9/17/2019 2:55:38 AM | Reply

    Hi Brian,
    I couldn't find the uwp project in source code on github.
    Is prism still support for UWP application?

  • Alb

    12/20/2019 2:37:55 PM | Reply

    Hi ,
    Great post.   Where in the process I can work with appsettings or appconfig files.  What are the options for logging.?  Is there any videos,  tutorials on working with DB?

    Thank you for all your help.
    Best Regards
    Alb.

  • Vincent Otieno

    2/8/2020 8:36:21 PM | Reply

    This saved me after hours of fruitless searching... Now I can show my dialogs using the new IDialogService. Thanks a million.

  • Rabi

    2/12/2020 7:31:22 PM | Reply

    I typically get PrismApplication examples targeted for full application. Is there any example for Class Library that we can use for Plugins but then get all benefits of Prism library for MVVM? Please share thoughts on how I can achieve this.

  • Rabi

    2/13/2020 4:38:01 PM | Reply

    I am wondering if there are examples for using Prism 7.* for Class Library projects where its not quite an Application on its own. Can anyone point me in the right direction?

Add comment

Loading