I have also mentioned how to solve errors most people have faced when starting with PRISM.
Prerequisites for this Project
Visual Studio 2010 onwards and Active Internet Connection (To download PRISM).
In this project I have used Visual Studio 2015.
What is PRISM?
Prism provides guidance to help you more easily design and build, flexible, and easy-to-maintain client business apps that run on Windows Runtime, Windows Presentation Foundation (WPF) desktop, Silverlight, or Windows Phone . These apps may start small and evolve over time.
Using design patterns that embody important architectural design principles, such as separation of concerns and loose coupling, Prism helps you to design and build apps that embody significant presentation and business logic that typically interact with back-end systems and services and, using a layered architecture, may be physically deployed across multiple tiers. It is expected that the app will evolve significantly over its lifetime in response to new requirements and business opportunities. In short, these apps are "built to last" and "built for change." Apps that do not demand these characteristics may not benefit from using Prism.
-MSDN
The article is divided into three parts:
- PART 1: Install PRISM and create a PRISM window.
- PART 2: Create UI Regions.
- PART 3: Create Modules and map them to predefined regions.
Legend:
- Focus of the step is highlighted in YELLOW.
- Error that could occur are in RED.
- Code and XAML starts with highlighted GREEN.
PART 1: Install PRISM and create a PRISM window
- Create a new WPF Application : Name = IntroToPrism
- Remove the default MainWindow.xaml
- Remove StartupUri="MainWindow.xaml" from the App.xaml file.
- Go to Tools - Nuget Package Manager - Package Manager Console.
- First we will add Prism.
- Type Install-Package Prism.Wpf -Version 6.1.0 and press enter.
- Then add Prism.Unity Package.
Type Install-Package Unity -Version 2.1.505.2. (Note: Specific versions are required)
- Then add Unity.Extensions,
Type Install-Package Prism.UnityExtensions -Version 4.1.0,
When using certain versions they may throw error asking for a specific version of Unity , to resolve add the requested version of Unity. Here I have used 2.1.505.2.
- Now add a new WPF Window and name it PrismAppShell.xaml.
- Create a new class Bootstrapper.cs.
- Then derive the Bootstrapper class from UnityBootstrapper class, i.e. UnityBootstrapper is now the base class.
Override two methods of UnityBootstrapper in Bootstrapper class.
InitializeModules
ConfigureModuleCatalog
Implement the abstract method CreateShell in Bootstrapper class.
You may have to add the following using statements also.
Code
- using Microsoft.Practices.Unity;
- using Microsoft.Practices.Prism.UnityExtensions;
- public class BootStrapper: UnityBootstrapper
- {
- protected override System.Windows.DependencyObject CreateShell()
- {
- return this.Container.Resolve < PrismAppShell > ();
- }
- protected override void InitializeModules()
- {
- base.InitializeModules();
- App.Current.MainWindow = (PrismAppShell) this.Shell;
- App.Current.MainWindow.Show();
- }
- protected override void ConfigureModuleCatalog()
- {
- base.ConfigureModuleCatalog();
- this.ModuleCatalog.AddModule(null);
- }
- }
PART 2 : Create UI Regions
- Now in PrismAppShell.xaml - add namespace
- xmlns:prism="http://www.codeplex.com/prism"
- Now we will create three regions Header, Body and Footer.
So add Content Controls to the window.
Give grid a name LayoutRoot
XAML:
- <Grid x:Name="LayoutRoot">
- <DockPanel LastChildFill="True" HorizontalAlignment="Stretch"
- Name="dockPanel1" VerticalAlignment="Stretch">
- <StackPanel Orientation="Horizontal" DockPanel.Dock="Top"
- Background="#FFCCD4F8" Height="25">
- <ContentControl prism:RegionManager.RegionName="HeaderRegion" Background="#FF93E094"></ContentControl>
- </StackPanel>
- <StackPanel Orientation="Horizontal" DockPanel.Dock="Left" Background="#FFD9E1EF" >
- <ContentControl prism:RegionManager.RegionName="BodyRegion" Background="#FFB5E4E1"></ContentControl>
- </StackPanel>
- <StackPanel Orientation="Horizontal" DockPanel.Dock="Bottom" Background="#FFD9E1EF" Height="25">
- <ContentControl prism:RegionManager.RegionName="FooterRegion" DockPanel.Dock="Bottom" Background="#FFF4B0B0"></ContentControl>
- </StackPanel>
- </DockPanel>
- </Grid>
- Now run the application.
In case you get any error on region, go to - References and delete Prism.dll
NOTE: You can create any combination of regions.
PART 3: Create Modules and map them the predefined regions, ie Header, Body and Footer
- Add new project PRISMMODULES and delete MainWindows.xaml again,
We will follow MVVM.
- Create a new folder View and add a new WPF window to the project and name it ToolbarWindow.xaml.
- Then create a new folder ViewModels and add a new class in it and name it ToolBarViewModel.cs.
- Then create a new folder Model and add a new class in it and name it ToolbarModule.cs.
- Goto the ToolbarModule.cs file and add the following code.
Code:
- using Microsoft.Practices.Prism.Modularity;
- using Microsoft.Practices.Prism.Regions;
- using System;
- using System.Collections.Generic;
- using System.ComponentModel;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- namespace PRISMMODULES.Model
- {
- public class ToolbarModule: IModule, INotifyPropertyChanged
- {
- private readonly IRegionViewRegistry regionViewRegistry = null;
- public ToolbarModule(IRegionViewRegistry regionViewRegistry)
- {
- this.regionViewRegistry = regionViewRegistry;
- }
- public void Initialize()
- {
- this.regionViewRegistry.RegisterViewWithRegion("HeaderRegion", typeof (ToolbarWindow));
- }
- public event PropertyChangedEventHandler PropertyChanged;
- }
- }
- Now go back to the BootStrapper.cs file and change method ConfigureModuleCatalog.
- protected override void ConfigureModuleCatalog()
- {
- base.ConfigureModuleCatalog();
- ModuleCatalog moduleCatalog = (ModuleCatalog)this.ModuleCatalog;
- moduleCatalog.AddModule(typeof(PRISMMODULES.Model.ToolbarModule));
- }
- Now if you try to run the application you may get an error, Window must be the root of the tree. Cannot add Window as a child of Visual. To solve this open the ToolbarWindow.xaml file and change the Window tag to UserControl and delete any Title property if any and also remove width and height.
- Now Goto ToolBarViewModel.cs and add the following for MVVM.
Code:
- using PRISMMODULES.Model;
- using System;
- using System.Collections.Generic;
- using System.ComponentModel;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- namespace PRISMMODULES.ViewModels
- {
- class ToolBarViewModel: INotifyPropertyChanged
- {
- public event PropertyChangedEventHandler PropertyChanged;
- private void RaisePropertyChanged(string propertyName)
- {
- if (PropertyChanged != null) PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
- }
- private ToolbarModule _TM;
- public ToolbarModule TM
- {
- get
- {
- return _TM;
- }
- set
- {
- _TM = value;
- RaisePropertyChanged("_TM");
- }
- }
- }
- }
- Now Goto ToolBarViewModel.cs.
Add Resource to point to the ViewModel and assign it as DataContext to the UserControl.
Now add a Toolbar to the Grid and give it width.
Add two buttons to the toolbar.
- <UserControl x:Class="PRISMMODULES.ToolbarWindow"
- xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
- xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
- xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
- xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
- xmlns:local="clr-namespace:PRISMMODULES"
- xmlns:mods="clr-namespace:PRISMMODULES.ViewModels"
- mc:Ignorable="d"
- DataContext="{Binding Source= mainViewModelLocator}">
- <UserControl.Resources>
- <ResourceDictionary>
- <mods:ToolBarViewModel x:Key="mainViewModelLocator"></mods:ToolBarViewModel>
- </ResourceDictionary>
- </UserControl.Resources>
- <Grid>
- <ToolBar Width="200" >
- <Button Content="Item 1 Test"/>
- <Button Content="Item 2 Try"/>
- </ToolBar>
- </Grid>
- </UserControl>
- Now run the Application,
And Voila !
Definitions
Dependency objects
Dependency object is the base object for all WPF objects. All the UI Elements like Buttons TextBox etc and the content elements like Paragraph, Italic, Span etc all are derived from Dependency Object.
Dependency objects are used for WPF property system. By default, what ever the property system we have in DOT Net CLR is very basic. But Dependency properties provide lots of additional features/services to support Data Binding.
Once you create any property as a dependency property, then automatically you get following feature implemented for you. ie. Change Notification, Validation, Call Back, Inheritance, DataBinding, Styles, Default Values etc.
Dependency injection
Dependency injection is a software design pattern that implements inversion of control for resolving dependencies. A dependency is an object that can be used (a service). An injection is the passing of a dependency to a dependent object (a client) that would use it. The service is made part of the client's state. Passing the service to the client, rather than allowing a client to build or find the service, is the fundamental requirement of the pattern.