Creating Application in Prism

Kailash Chandra Behera | Sunday, May 08, 2016

Introduction

This article explains an illustration of creating a windows application in Prism Library(WPF Prism). The solution includes recommended practices and techniques and is the basis for the procedures in Prism. This illustration created in the Visual Studio 2012, It can also developed in the visual studio 2008 and 2010, because wpf supports from .net framework 3.5 to latest version.

  • Microsoft.Practices.Prism.dll.
    This assembly contains the implementation of the Prism Library core components such as modularity, logging services, communication services, and definitions for several core interfaces. It also contains the implementation of Prism Library components that target WPF applications, including commands, regions, and events.

  • Microsoft.Practices.Prism.UnityExtensions.dll.

    This assembly contains base and utility classes you can reuse in applications built with the Prism Library that consume the Unity Application Block. For example, it contains a bootstrapper base class, the UnityBootstrapper class, that creates and configures a Unity container with default Prism Library services when the application starts.

  • Microsoft.Practices.Unity.dll

    This assembly enables you to use the Unity Application Block in your application. By default, applications built using Prism use the Unity Application Block or MEF. However, developers who prefer to use different container implementations can build adapters for them using the provided extensibility points in the Prism Library.

  • Microsoft.Practices.ServiceLocation.dll.

    This assembly contains the Common Service Locator interface used by Prism to provide an abstraction over Inversion of Control containers and service locators; therefore, you can change the container implementation with ease.

This below steps have covered all the things to build prism application. follow the steps to complete the things

Steps:

  1. Initial Steps(Create a solution with a shell project)

    In this task, you create the initial Visual Studio solution and add a WPF Application project that is the basis of solutions built using Prism Library. This project is known as the shell project.

  2. Set up the shell .In this task, you set up a window, the shell window, to host different user interface (UI) components in a decoupled way.
  3. Set up the application's bootstrapper.In this task, you set up code that initializes the application.
  4. Add Module.In this task, you will create a module and add it to your solution. A module in Prism is a logical unit in your application.
  5. Add View.

  6. Add a region to the shell .

    The task describes how to add an ItemsControl control to the shell window and associate a region to it. In a subsequent task, you will dynamically add a view to this region.

Step-1: Initial Steps (Create a solution with a shell project)

  • Open Visual Studio, create a new WPF application. To do this, point to New on the File menu, and then click Project.

  • In the Project types list, select Windows inside the Visual C# node. In the Templates box, click WPF Application. Finally, set the project's name to simpleprismapplication, specify a valid location, and then click OK.

  • Using Windows Explorer, create a folder named PrismLibrary inside your solution's folder, and then copy the above assemblies(Prism libraries) into it (they are located in the Source\bin\ PrismLibrary folder):

  • In the simpleprismapplication project, add references to the assemblies listed in the above.

  • The shell window is the top-level window of an application based on the Prism Library. This window is a place to host different UI components that exposes a way for itself to be dynamically populated by others, and it may also contain common UI elements, such as menus and toolbars. The shell window sets the overall appearance of the application.

Step-2: Set up the shell

  • In Solution Explorer, rename the file MainWindow.xaml to Shell.xaml.

  • Open the code-behind file Shell.xaml.cs and rename the MainWindow class to Shell . If the Preview Changes – Rename dialog box appears, click Apply.

  • MainWindow renaming using Visual Studio refactoring tools

  • In XAML view, open the Shell.xaml file, and then set the following attribute values to the Window root element:

    • x:Class = "simpleprismapplication.Shell" (this matches the code behind class's name)

    • Title = "Simple Prism Application"

    Your code should look like the following.

     <Window x:Class="simpleprismapplication.Shell"  
     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"  
     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"  
     xmlns:prsm="http://www.codeplex.com/prism"      
      Title="Simple Prism Application" Height="350" Width="525">    
     <Grid >  
     </Grid>  
     </Window>  
    

  • In the Shell.xaml file, add the following namespace definition to the root Window element. You need this namespace to use an attached property for regions that is defined in the Prism Library.

     xmlns:prism="http://www.codeplex.com/prism"  
    

  • Replace the Grid control in the shell window with an ItemsControl control named MainRegion, as shown in the following code.

     <Window x:Class="simpleprismapplication.Shell"  
     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"  
     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"  
     xmlns:prsm="http://www.codeplex.com/prism"      
      Title="Simple Prism Application" Height="350" Width="525">    
     <ItemsControl Name="MainRegion" prsm:RegionManager.RegionName="MainRegion" >  
     </ItemsControl>  
     </Window>  
    

  • In the ItemsControl control definition, set the attached property prism:RegionManager.RegionName to "MainRegion", as shown in the following code. This attached property indicates that a region named MainRegion is associated to the control.

     <ItemsControl Name="MainRegion" prsm:RegionManager.RegionName="MainRegion" >  
     </ItemsControl>  
    

  • When the shell window is instantiated, WPF resolves the value of the prism:RegionManager.RegionName attached property and invokes a callback in the RegionManager class. This callback creates a region and associates it with the ItemsControl control.

Step-3: Set up the application's bootstrapper

  • Add a new class file named Bootstrapper.cs to the simpleprismapplication project.

  • Add the following using statements at the top of the file. You will use them to refer to elements referenced in the UnityBootstrapper class.

     using System.Windows;  
     using Microsoft.Practices.Prism.Modularity;  
     using Microsoft.Practices.Prism.UnityExtensions;  
     using Microsoft.P
    

  • Update the Bootstrapper class's signature to inherit from the UnityBootstrapper class.

     class Bootstrapper : UnityBootstrapper  
     {  
     }  
    

  • Override the CreateShell method in the Bootstrapper class. In this method, create an instance of the shell window and return it, as shown in the following code

     protected override DependencyObject CreateShell()  
      {  
         return new Shell();   
      }  
    

  • You return the shell object to have the UnityBootstrapper base class attach an instance of the region manager service to it. The region manager service is a service included in the Prism Library that manages regions in the application. By having a region manager instance attached to the shell window, you can declaratively register regions from XAML code that will exist in the scope of the shell window and child views.

  • Override the InitializeShell method in the Bootstrapper class. In this method, display the shell to the user.

     protected override void InitializeShell()  
      {  
           base.InitializeShell();  
            App.Current.MainWindow = (Window)this.Shell;  
           App.Current.MainWindow.Show();  
     }  
    

  • Override the ConfigureModuleCatalog method. In this template method, you populate the module catalog with modules. The module catalog interface is Microsoft.Practices.Prism.Modularity.IModuleCatalog, and it contains metadata for all the modules in the application. Because the application contains no modules at this point, the implementation of the ConfigureModuleCatalog method should simply call the base implementation and return. You can paste the following code in your Bootstrapper class to implement the method.

     protected override void ConfigureModuleCatalog()  
     {  
           base.ConfigureModuleCatalog();  
     }  
    

  • Open the file App.xaml.cs and initialize the Bootstrapper in the handler for the Startup event of the application, as shown in the following code. By doing this, the bootstrapper code will we executed when the application starts.

     public partial class App : Application  
     {  
              protected override void OnStartup(StartupEventArgs e)  
              {  
                      base.OnStartup(e);  
                     Bootstrapper bootstrapper = new Bootstrapper();  
                      bootstrapper.Run();  
              }  
     }  
    
  • Open the file App.xaml.cs and initialize the Bootstrapper in the handler for the Startup event of the application, as shown in the following code. By doing this, the bootstrapper code will we executed when the application starts.

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

  • Open the App.xaml file and remove the attribute StartupUri. Because you are manually instantiating the shell window in your bootstrapper, this attribute is not required. The code in the App.xaml file should look like the following.

     <Application x:Class="simpleprismapplication.App"  
            xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"  
            xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">  
                 <Application.Resources>  
                 </Application.Resources>  
          </Application>  
    

  • Build and run the application. You should see an empty Simple Prism Application window, as shown in the following illustration.

Step-4: Adding Module to Project

  • Add a new class library project to your solution. To do this, right-click the simpleprismapplication solution node in Solution Explorer, point to Add, and then click New Project. In the Project types list, select Windows in the Visual C# node. In the Templates box, click Class Library. Finally, set the project's name to simpleprismapplicationModule, and then click OK. The following illustration shows your solution. Solution with a module named simpleprismapplicationmodule

  • Add references in your module to the following WPF assemblies. To do this, right-click the simpleprismapplicationmodule project in Solution Explorer, and then click Add Reference. In the Add Reference dialog box, click the .NET tab, click the following assemblies, and then click OK:

    • PresentationCore.dll

    • PresentationFramework.dll

    • WindowsBase.dll

    • System.Xaml.dll

  • Add references in your module to the Microsoft.Practices.Prism.dll Prism Library assemblies. To do this, right-click the HelloWorldModule project in Solution Explorer, and then click Add Reference. In the Add Reference dialog box, click the Browse tab, click the following assemblies, and then click OK:

  • Rename the Class1.cs file to simpleprismapplicationmodule.cs. Open the file simpleprismapplicationmodule.cs and impliment the Microsoft.Practices.Prism.Modularity namespace in the class.

  • Implement the IModule interface in simpleprismapplicationmodule class and create the constructure of this class.

  • Add a folder having name View to store the views of your application to the simpleprismapplicationmodule project again add one more folder and rename to ViewModel to store the viewmodels.

  • Note:- Above mentioned folders can be created in the simpleprismapplication project also, but it is good pratice to create a separate module to for manage code

  • In your shell project, add a reference to the simpleprismapplicationmoduleproject. Open the Bootstrapper.cs file and explore the ConfigureModuleCatalog method. The method implementation is shown in the following code.

     protected override void ConfigureModuleCatalog()  
     {  
           base.ConfigureModuleCatalog();  
     }  
    

  • Update the ConfigureModuleCatalog method to register the simpleprismapplicationmodule module with the module catalog instance. To do this, you can add bellow codes in the ConfigureModuleCatalog bellow the base.ConfigureModuleCatalog(); code.

     ModuleCatalog moduleCatalog = (ModuleCatalog)this.ModuleCatalog;  
      moduleCatalog.AddModule(typeof(simpleprismapplication.simpleprismapplicationmodule));  
    

  • now addin module and register module application with shell is done next steps is adding view to application

Step-5: Adding a View

Add a new WPF user control to your module by right clicking the Views folder in Solution
Explorer, point to Add, and then click New Item. In the Add New Item dialog box, select the User
Control (WPF) template, set the name to simpleprismapplicationview.xaml, and then click Add.
Add a text block to the view. and set the text to "Hellow Friends" and Save the file.

 <UserControl x:Class="simpleprismapplicationmodule.View.simpleprismapplicationview"       xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"       xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"       xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"       xmlns:d="http://schemas.microsoft.com/expression/blend/2008"       mc:Ignorable="d"       d:DesignHeight="300" d:DesignWidth="300">  
   <Grid>  
     <TextBlock Text="Hellow Friends" Foreground="Green" HorizontalAlignment="Center" VerticalAlignment="Center" FontFamily="Calibri" FontSize="24" FontWeight="Bold"></TextBlock>  
   </Grid>  
 </UserControl>  

Step-6Add a region to the shell

  • Open the simpleprismapplicationmodule.cs file. Add the following using statement to the top of the file. You will use it to refer to the region elements in the Prism Library.

     using Microsoft.Practices.Prism.Regions;  
    

  • Create a private read-only instance variable to hold a reference to the region manager and paste the following code inside the class body.

     using Microsoft.Practices.Prism.Regions;  
    

  • Add the constructor of the simpleprismapplicationmodule class to obtain a region manager instance through constructor dependency injection and store it in the regionManager instance variable. The constructor has to take a parameter of type Microsoft.Practices.Prism.Regions.IRegionManager. You can paste the following code inside the class body to implement the constructor.

     public simpleprismapplicationmodule(IRegionManager regionManager)  
     {  
           this.regionManager = regionManager;   
     }  
    

  • In the Initialize method, invoke the RegisterViewWithRegion method on the RegionManager instance. This method registers a region name with its associated view type in the region view registry; the registry is responsible for registering and retrieving of these mappings.The RegisterViewWithRegion method has two overloads. When you want to register a view directly, you use the first overload that requires two parameters, the region name and the type of the view.

     public void Initialize()  
         {  
              regionManager.RegisterViewWithRegion("MainRegion",typeof(View.simpleprismapplicationview));  
         }  
    

  • The UI composition approach used in the preceding code is known as view discovery. When using this approach, you specify the views and the region where the views will be loaded. When a region is created, it asks for its associated views and automatically loads them.

    Note: The region's name must match the name defined in the RegionName attribute of the region.

Related Articles

  1. Resource in WPF
  2. UpdateSourceTrigger
  3. WPF Binding
  4. WPF Style
  5. WPF Converters
  6. WPF MultiValue Converter
  7. WPF Value Converters
  8. WPF INotifyPropertyChanged Interface
  9. WPF DEPENDENCY PROPERTIES

Summery

In this discussion we have seen how to create a new application in wpf using prism library. Hope
this article is need full to you


Thanks