Creating a simple WPF application using Prism: Part 1


How to build a simple WPF application with Prism….this post is going to be just that. If you are unclear about the architecture of Prism, do have a quick look at it on my previous post.

Wpf_Prism_app

First off, create a WPF Application Project, which will be the shell. The shell is a container which will essentially host your UI controls in your Modules, and can be separated into different Regions. From the screenshot above, I have created my shell with 3 different Regions. So let’s create a Shell.xaml (Window) in this new project, and to define the 3 Regions, you can use ContentControl, and mark each of them uniquely with string names. I’ve encapsulated my Region Names into a static class so I can use them elsewhere later.

xmlns:Regions="clr-namespace:Microsoft.Practices.Composite.Presentation.Regions;assembly=Microsoft.Practices.Composite.Presentation"
xmlns:Infra="clr-namespace:WpfComposite.Infrastructure;assembly=WpfComposite.Infrastructure"

<contentcontrol Grid.Row="0" Grid.ColumnSpan="2"
                Regions:RegionManager.RegionName="{x:Static Infra:RegionNames.SearchRegion}"/>
<contentcontrol Grid.Row="1" Grid.Column="0" Regions:RegionManager.RegionName="{x:Static Infra:RegionNames.NavigationRegion}" />
<contentcontrol Grid.Row="1" Grid.Column="1" Regions:RegionManager.RegionName="{x:Static Infra:RegionNames.EntityRegion}" />

Next you will need a BootStrapper class to handle the starting of the application, because we are going to be using Prism to do all the magically stuff. This BootStrapper class inherits from the UnityBootStrapper in the Prism library.

public class BootStrapper : Microsoft.Practices.Composite.UnityExtensions.UnityBootstrapper
{
    protected override System.Windows.DependencyObject CreateShell()
    {
        Shell shell = new Shell();
        shell.Show();
        return shell;
    }

    protected override Microsoft.Practices.Composite.Modularity.IModuleCatalog GetModuleCatalog()
    {
        ModuleCatalog catalog = new ModuleCatalog();
        catalog.AddModule(typeof(NavigationModule));
        catalog.AddModule(typeof(WpfComposite.Search.SearchModule));
        catalog.AddModule(typeof(EntityModule));
        return catalog;
    }
}

In the Initialize method, we are creating a ModuleCatalog. Think of the catalog as a store of all the Modules that we are going to need to inject into our shell. In this scenario I have 3 Modules created to cater for my three different Regions, so we are loading these Modules programmatically. Note that you will need to reference the 3 Dlls in your project in order to add the Modules. It’s not a must to reference these Dlls to load the Modules, but we are doing this here for simplicity. If you’re just creating one from scratch, you only need to return a new instance of ModuleCatalog.

Next you need to modify the App.xaml.cs file and overwrite the OnStartUp method. You will also need to remove the StartupUri property from the App.xaml file.

public partial class App : Application
{
    protected override void OnStartup(StartupEventArgs e)
    {
        base.OnStartup(e);
        new BootStrapper().Run();
    }
}

You will be requiring the following Prism libraries to be referenced in this project, and any others that will be created:

  • Microsoft.Practices.Composite
  • Microsoft.Practices.Composite.Presentation
  • Microsoft.Practices.Composite.UnityExtensions
  • Microsoft.Practices.ServiceLocation
  • Microsoft.Practices.Unity
  • Microsoft.Practices.ObjectBuilder2 (sometimes you need this, not always)

VS_ShellIf you followed these steps closely, you should have a structure like the one on the left.

Hopefully everything should compile.

Press F5, and you should be able to see your blank shell application start up.

In my next post, I will continue this walkthrough and explain how to go about creating your Modules and MVVM pattern.

Share this post:
Advertisements
Posted in WPF. Tags: . 1 Comment »

One Response to “Creating a simple WPF application using Prism: Part 1”

  1. Creating a simple WPF application using Prism: Part 2 « Code Blitz Says:

    […] Creating a simple WPF application using Prism: Part 1 […]


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: