Jagamisviis:


How to: Show a View in a Region Using View Discovery UI Composition

Overview

By using regions, you can dynamically add views to a known location in the application without being tightly coupled to the containing view. This topic describes how to place a view in any region. The code in this topic could reside in the module initialization or a controller class that coordinates the activity of multiple views.

Note

This topic assumes you are familiar with regions. For more information about regions, see the UI Composition technical concept.

Prerequisites

You must have a solution with a region built using the Composite Application Library.

For information about how to create a solution with the Composite Application Library, see How to: Create a Solution Using the Composite Application Library.

For information about how to add a region to your solution, see How to: Add a Region.

Steps

To show a view in a region

  1. Obtain a reference to the region manager instance. A region manager is a class that implements the IRegionManager interface. You can obtain the reference by using dependency injection, as shown in the following code (in this example, the code is written in a module initializer class; it is assumed that the class is instantiated using a dependency injection container).

    public class Module : IModule
    {
        private readonly IRegionManager regionManager;
    
        public Module(IRegionManager regionManager)
        {
            this.regionManager = regionManager;
        }
    
        ...
    
    }
    
  2. Invoke the RegisterViewWithRegion method on the region manager instance to register a mapping that associates a region with a view. Typically, you register mappings in the Initialize method of the module that contains the view.

    The RegisterViewWithRegion method has the following two overloads:

    • RegisterViewWithRegion(string regionName, Type viewType);
    • RegisterViewWithRegion(string regionName, Func<object> getContentDelegate);

    If you want to register a view directly, use the first overload. Keep in mind that to use this overload, the view has to be referenced in your class. The following code example shows an example of this.

    public void Initialize()
    { 
        this.regionManager.RegisterViewWithRegion("MainRegion",
                typeof(EmployeesView));
    }
    

    If you want to provide a delegate, for example to resolve the presenter that is responsible for creating the view in a presenter-first approach, use the second overload. The following code shows how to register the mapping that associates a region named MainRegion to a view using the second overload of the RegisterViewWithRegion method.

    public void Initialize()
    { 
        this.regionManager.RegisterViewWithRegion("MainRegion",
                () => this.container.Resolve<EmployeesPresenter>().View);
    }
    

    In the preceding code, Presenter First development is used. Therefore, the presenter is responsible for creating the view as seen in the lambda expression.

    Note

    The region's name must match the name defined in the RegionName attribute of the region. For more information about configuring regions, see How to: Add a Region.

Outcome

When a region is created, it looks for its associated views in the registry. The matching views are automatically pulled and loaded inside the region.

More Information

For information related to regions and views, see the following topics:

For a complete list of How-to topics included with the Composite Application Guidance, see Development Activities.

Home page on MSDN | Community site