How to: Show a View in a Region Using View Injection UI Composition
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.
|This topic assumes you are familiar with regions. For more information about regions, see the UI Composition technical concept.|
If you want to show a view in a scoped region, see How to: Show a View in a Scoped Region.
A solution built using the Composite Application Library with a region.
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.
To show a view in a region
- 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).
- Obtain the region where you want to place your view. To do this, use the Regions property on the region manager instance, passing the region name as a parameter. The following code shows how to obtain a region named "MainRegion".
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.
- Create an instance of the view you want to show, and then add it to the region. To do this, invoke the Add method on the region instance, as shown in the following example code.
Note: The Add method adds the view to the region, but it does not ensure that the view is displayed. For example, if the underlying user interface (UI) element of the region is a TabControl control, the view will be added to the control as a new tab, but the tab will not necessarily receive focus.
- If you want to make sure the view is displayed in the UI, invoke the Activate method on the region, passing the view object as the parameter. For example, if the underlying UI element of the region is a TabControl control, the tab that hosts the view will receive focus.
The following code shows how the view added in the previous step is activated.
In some cases, you may want to add a view to a region and associate a name to it. By associating a name to a view, you can retrieve the view later from the region to perform actions with it, such as removing the view from the region, showing it, or editing it. Views that have an associated name are referred to as named views.
To add a named view to a region
- Invoke the method Add on the region, passing as parameters the view instance and the view name, as shown in the following code.
To retrieve the view instance by its name
- Invoke the GetView method on the region, passing the view's name as the parameter, as shown in the following code.
|If a view with the specified name is not found in the region, the default region implementation included in the Composite Application Library returns a null value. Therefore, you can use the GetView method to test if a view exists in a region.|
After you add the view and show it to the region, the view appears on screen in the region.
For information related to regions and views, see the following topics:
- How to: Add a Region
- How to: Create a Custom Region Adapter
- How to: Show a View in a Region Using View Discovery UI Composition
- How to: Show a View in a Scoped Region
For a complete list of How-to topics included with the Composite Application Guidance, see Development Activities.