Add a Region to the Shell in WPF PRISM

Introduction

follow the below steps to add region into your application

1. Open the applicationmodule.cs file of your module project . 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.

C# Code

using Microsoft.Practices.Prism.Regions;

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

C# Code

using Microsoft.Practices.Prism.Regions;

3. Add the constructor of the applicationmodule 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. C# Copy Code

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

4. 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.

C# Code

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

5. 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.