Prism Learning (1), prism Learning

Source: Internet
Author: User

Prism Learning (1), prism Learning

Reprinted from:

This article describes the use of Region in Prism.

Download this Demo

Here we unify some names in prism.

1. Shell main program container

2. Region content area

3. Module

4. wpf is not a special case, that is, wpf and silverlight.

I. wpf Content Control

It inherits from the ContentControl control, which is called a content control.

<ContentControl Content=""></ContentControl>


The ContentControl control defines a Content. Without a framework, it can also be used as a Content area. however, to meet the needs of the ui, we also need a variety of controls for content areas, such as TabControl, DockPanel, Selector, etc. Some controls inherit from ItemsControl, which belongs to the set control and does not belong to the content control. However, they can be used as containers according to different requirements, but their usage is different.

To unify the operations on the content area, prism provides an adaptation mode. It can also be said that the ing between controls and Region is provided. the operation methods of controls that can be used as containers are unified as Region operations.

Prism has three built-in controls that can be used as content area adaptation objects.

It seems that there are only three widgets, but as long as they are other widgets inherited from the three widgets, the following describes how to use them.

Ii. Basic Region operations. The following uses Hello World as an Example 1. register the content area in Shell

The following code: the name space of the prismregistration.

RegionName, an additional Property of RegionManager, is used to register a content region named MainRegion.

<Window x:Class="HelloWorldSample.Shell"    xmlns=""    xmlns:x=""    xmlns:cal=""    Title="Composite Application Library Sample" Width="400" Height="300">    <ContentControl cal:RegionManager.RegionName="MainRegion"/></Window>
2. Use IRegionManager to obtain the registered content area

Registered Region will be saved in the Regions set of IRegionManager. Retrieve the Region according to the registered name and an IRegion object will be returned.

IRegion mainRegion = this.regionManager.Regions["MainRegion"];


3. add content to Region


First define a user control

<UserControl x:Class="HelloWorld.Views.HelloWorldView"    xmlns=""    xmlns:x="">    <Grid>        <TextBlock Text="Hello World" Foreground="Green"                    HorizontalAlignment="Center" VerticalAlignment="Center"                    FontFamily="Calibri" FontSize="24" FontWeight="Bold"></TextBlock>    </Grid></UserControl>


Add a user control using the Add method of IRegion. The following is the code of a simple module. When this module is loaded, the Initialize method is executed by default.

public class HelloWorldModule : IModule{    private readonly IRegionManager regionManager;    public IUnityContainer container { get; set; }    public HelloWorldModule(IRegionManager regionManager, IUnityContainer container)    {        this.regionManager = regionManager;        this.container = container;    }    public void Initialize()    {        RenderHelloWorldView();     }    void RenderHelloWorldView()    {        IRegion mainRegion = this.regionManager.Regions["MainRegion"];        mainRegion.Add(new HelloWorldView());    }}


The meaning of the above three steps.

Iii. Region operations on views


1. you can perform the following operations as a class. the Add method has three reloads. The third method is explained below. when you specify a name for the View, you can use the GetView method to obtain the View and then delete it using the Remove Method.

IRegionManager Add (object view); IRegionManager Add (object view, string viewName); IRegionManager Add (object view, string viewName, bool createRegionManagerScope); void Remove (object view ); object GetView (string viewName); 2. activate and disable a View
By default, when a View is added to Region, it is recorded as active. IRegion provides two sets and two methods to control the activity status of the View. the Activate method changes the View to the active state, and the Deactivate method freezes the use of the View. the call of the two methods changes the attributes of the Views and ActiveViews sets. IViewsCollection implements the INotifyCollectionChanged interface. Therefore, when the set changes, an event is triggered to trigger ui changes. This event is triggered by the Region and control adapter. how to customize the RegionAdapter will be introduced next time. It will be very clear here later.
        IViewsCollection Views { get; }        IViewsCollection ActiveViews { get; }        void Activate(object view);        void Deactivate(object view);   
4. register the sub-Region


1. View added in Region can also be registered, but the added View must rely on the dependency injection function. See the following example.


<UserControl x:Class="HelloWorld.Views.HelloWorldViewAgain"    xmlns=""    xmlns:x=""    xmlns:cal=""    Height="300" Width="300">    <Grid>        <StackPanel>            <TextBlock Text="HelloWorldViewAgain"></TextBlock>            <TextBlock Text="HelloWorldView Loading"></TextBlock>            <ItemsControl cal:RegionManager.RegionName="HelloWorldViewAgain"/>            <TextBlock Text="HelloWorldView Loaded"></TextBlock>        </StackPanel>    </Grid></UserControl>



Add HelloWorldViewAgain in Shell Region

void RenderHelloWorldViewAgain(){    IRegion mainRegion = this.regionManager.Regions["MainRegion"];    HelloWorldViewAgain viewAgain = container.Resolve<HelloWorldViewAgain>();    mainRegion.Add(viewAgain, "MainRegion");    viewAgain.DisplayHelloWorldView();}


The DisplayHelloWorldView method adds HelloWorldView to the Region of HelloWorldViewAgain defined

public void DisplayHelloWorldView(){    IRegion secondRegion = this.regionManager.Regions["HelloWorldViewAgain"];    secondRegion.Add(new HelloWorldView());}

Expressed the meaning of the above steps


2. Set the RegionManager management scope

By default, when RegionManager registers a Region, RegionManager adds it to its Regions set.

For example

<ContentControl x: Name = "panel" cal: RegionManager. RegionName = "MainRegion"/>

This Regions attribute is an additional attribute of ContentControl. You can use the GetRegionManager method of RegionManager to obtain the Region set of the control.

Let's look back at the Add method above.

IRegionManager Add(object view, string viewName, bool createRegionManagerScope);



When adding a View to Region, you can specify whether to reset the RegionManager range. if this parameter is set to True, the CreateRegionManager method is called to recreate a RegionManager for the current View. this shows that if a Region is registered in the created View, you do not know which RegionManager it exists in.

Now we can change the preceding RenderHelloWorldViewAgain method again. When adding a View,

The third parameter is set to True, as shown below:

void RenderHelloWorldViewAgain(){    IRegion mainRegion = this.regionManager.Regions["MainRegion"];    HelloWorldViewAgain viewAgain = container.Resolve<HelloWorldViewAgain>();    mainRegion.Add(viewAgain, "HelloWorldViewAgain",true);    viewAgain.DisplayHelloWorldView();}


At this time, it is difficult to know which RegionManager The View registered Region of HelloWorldViewAgain. Now the DisplayHelloWorldView method is changed as follows:

public void DisplayHelloWorldView(){    var view=this.regionManager.Regions["MainRegion"].GetView("HelloWorldViewAgain") as DependencyObject;    IRegion region=RegionManager.GetRegionManager(view).Regions["HelloWorldViewAgain"];    region.Add(new HelloWorldView(), "hello");}


This method can avoid problems such as duplicate Region names, but it is also troublesome to obtain RegionManager of Region.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.