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

  • Karel Král

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

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

  • 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

  • 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?

  • 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

  • Arulraj

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

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

Add comment

Loading