Region of Prism (1)

Source: Internet
Author: User

Http://www.cnblogs.com/li-xiao/archive/2011/01/31/1947163.html

Prism can help us develop ModularizationProgramSeparate programs into independent modules for development. Then, when the program runs, the modules are combined to provide various functions for the program. Generally, a module is a set of views and functions. Therefore, a method is required to present these views at a specific time in some form. Prism uses shell + region to organize the layout of views and convert views.

 

 

As shown in, shell is equivalent to the master page in ASP. NET. It defines the layout and topic of the page. The navigation area and content area are reserved parts that need to be filled with content, that is, region, which serves as a placeholder. during runtime, the program dynamically fills the content in region.

So how can we define a region as region?

First, introduce the prism namespace.

Xmlns: prism = "http://www.codeplex.com/prism" If ide cannot find this namespace, You need to register prism first.

Add the attached property to the control to be defined as region.

<ContentcontrolPrism: regionmanager. regionname = "mainregion"/>

Not all controls can be used as region. You need to add a regionadapter for the control to be defined as region. Regionadapter is used to create the corresponding region for a specific control, bind the control to the region, and then add some actions for the region. A regionadapter needs to implement the iregionadapter interface. If you need to customize a regionadapter, you can save some work by inheriting the regionadapterbase class. Prism provides several regionadapters for Silverlight:

    • Contentcontrolregionadapter: Creates a singleactiveregion and binds it to contentcontrol.
    • Itemscontrolregionadapter: creates an allactiveregion and binds it to itemscontrol.
    • Selectorregionadapter: Creates a region and binds it to the selector.
    • Tabcontrolregionadapter: Creates a region and binds it to tabcontrol.

As you can see, the four views in the navigationregion corresponding to the navigation area are highlighted, while only one of the four views in the contentregion corresponding to the content area is highlighted (orange indicates that the views are displayed on the page ). Itemscontrol is originally composed of many items. Therefore, itemscontrolregionadapter creates allactiveregion. All active views of this type of region are displayed in itemscontrol, while contentcontrol can only accommodate one content, therefore, contentcontrolregionadapter creates a singleactiveregion. Only one of the Views is active and will be displayed in contentcontrol. Others are invisible and need to be activated (active ), to display it.

Generally, we do not directly deal with region, but through regionmanager, which implements the iregionmanager interface. The iregionmanager Interface contains a read-only attribute regions, which is a collection of region and a createregionmanager method. Prism uses the regionmanagerextensions class to add more features to iregionmanager using the extension method.

    • Addtoregion: Add a view to a region.
    • Registerviewwithregion: associate a view with a region. When region is displayed, the associated view is displayed. That is to say, the associated view is not created before this region is displayed.
    • Requestnavigate: Performs page switching and switches the view displayed in the specified region to the specified view.

As mentioned at the beginning of this article, you need to display the views scattered across modules at specific locations on the page at runtime. First, you need to define the display area of the page, that is, region. Then define the time and method for creating a view. There are two ways to define the ing relationship between views and region in Prism: View discovery and view injection.

View discovery establish the relationship between region and view in declarative mode. For example, in the navigation area, you need to fill in the navigation views when they are displayed in the navigation area. A default content view is also required in the content area. You can also understand view discovery in this way, that is, to specify a default view of region. We can use the iregionmanager. registerviewwithregion method to declare which view a region should display by default. Note that this is register, which is registration, that is, the view will not be created immediately. When region is displayed on the page, it searches for the view associated with itself and initializes it.

The advantage of this is that we don't have to worry about when to create a view, and everything will be done automatically. The disadvantage is that the default view is determined. This method does not work when views need to be converted. In this case, view injection is required.

View injection gives us more precise control over the views displayed in region. Generally, you can add a view to a region by calling the iregionmanager. addtoregion method or the iregionmanager. Regions ["regionname"]. Add method.Instance. For singleactiveregion (contentcontrolregionadapter creates this type of region), you can use the iregion. Activate method to display a view that has been added to region. Of course, you can also use the iregion. Deactivate method to set the view status to non-active or simply call the iregion. Remove Method to remove the view. As you can see, because you want to add a view instance, You need to carefully design when to use view injection to avoid unnecessary overhead.

Some navigation APIs are added in Prism 4.0. This API greatly simplifies the view injection process. It uses URI to navigate the View Graph in region, and then creates a view based on Uri, add it to region, and then activate the view. The navigation API is not only used to simplify the view injection process, but also provides forward and backward functions and provides good support for navigation in mvvm mode, parameters can also be passed during navigation. Therefore, we recommend that you use the new navigation API, that is, the iregionmanager. requestnavigate method.

If a page is relatively unchanged, such as the navigation area, it will not be changed easily after the initialization process of the program. At this time, it is more suitable to use the registerviewwithregion method, this process can usually be completed in the module initialize method.

Public void initialize () {logger. log ("initialize the navigation module", category. debug, priority. low); _ regionmanager. registerviewwithregion (regionnames. navregion, typeof (navigationitem); _ regionmanager. registerviewwithregion (regionnames. mainregion, // both methods can be () => _ container. resolve <navigationcontainer> (); _ regionmanager. registerviewwithregion (regionnames. navdemoactionregion, typeof (actioncontroller ));}

If you need to switch pages frequently in a region, such as the main content area, you can use view injection.

Iregionmanager regionmanager =...; iregion mainregion = regionmanager. Regions ["mainregion"]; inboxview view = This. Container. Resolve <inboxview> (); mainregion. Add (View );

We can see that the view instance has been generated at this time. As mentioned before, a region can contain multiple views which are in different States. For region of itemscontrol type, many items are displayed, therefore, you can add it. However, for region such as contentcontrol, only one view can be displayed at a time. Therefore, an activate process is required after adding it.

To use URI for navigation, you only need to provide the name of the view to be switched. You do not need to know the type of the view to achieve decoupling, and you can use URI to transmit parameters.

 
Public void initialize () {// because prism cannot determine the type of each view, the object is used. Therefore, iservicelocator is used when obtaining an instance based on viewname. getinstance <Object> (viewname)
_ Container. registertype <object, viewa> (viewnames. viewa); _ container. registertype <object, viewb> (viewnames. viewb); _ container. registertype <object, viewc> (viewnames. viewc );}

First, register the view type to associate the view name with the view type. You can call the requestnavigate method during navigation.

 
Void tospecifiedview (string viewname) {URI uri = new uri (viewname, urikind. relative); _ regionmanager. requestnavigate (regionnames. navdemoshowregion, Uri); logger. log ("Jump to view [" + viewname + "]", category. info, priority. low );}

Prism provides the uriquery class to help us PASS Parameters during navigation.

Void tospecifiedview (string viewname) {uriquery query = new uriquery (); If (viewname = viewnames. viewa) {query. add ("time", datetime. now. tow.timestring ();} uri = new uri (viewname + query. tostring (), urikind. relative); _ regionmanager. requestnavigate (regionnames. navdemoshowregion, Uri, callbackhandler); // you can add or remove the callback method}

The aboveCodeWhen you jump to viewa, pass a parameter called time. So how can we get the passed parameters in the view? Here we will mention the inavigationaware interface. This interface allows the view or its corresponding viewmodel to participate in the page navigation process. Therefore, this interface can be implemented either by the view or by the datacontext of the view, which usually refers to the viewmodel.

Public interface inavigationaware {bool isnavigationtarget (navigationcontext); void onnavigatedto (navigationcontext); void onnavigatedfrom (navigationcontext );}

When you go from this page to another page, the onnavigatedfrom method is called, and the navigationcontext contains the URI of the target page.

When navigating from other pages to this page, isnavigationtarget is called first, and isnavigationtarget returns a bool value. Simply put, this method is used to tell prism, whether to reuse this view or create another instance. Call the onnavigatedto method. When you navigate to this page, you can retrieve the passed parameters from navigationcontext.

Another advantage of using the navigation API is that you can move the page forward and backward, and everything is done by prism. This function is provided by the iregionnavigationjournal interface.

Public interface iregionnavigationjournal {bool cangoback {Get;} bool cangoforward {Get;} implements currententry {Get;} inavigateasync navigationtarget {Get; set;} void clear (); void Goback (); void goforward (); void recordnavigation (iregionnavigationjournalentry entry );}

The cangoback and cangoforward attributes indicate whether the current status can be backward or forward. If possible, you can use the Goback and goforward methods to forward and backward.

Public class actioncontrollerviewmodel: icationicationobject {private iregion _ demoshowregion; Public bool cangoback {get {return _ demoshowregion. navigationservice. journal. cangoback ;}} public bool cangoforward {get {return _ demoshowregion. navigationservice. journal. cangoforward;} void toprevious () {_ demoshowregion. navigationservice. journal. goback (); resetnavigationbuttonstate ();} void tonext () {_ demoshowregion. navigationservice. journal. goforward (); resetnavigationbuttonstate ();} void resetnavigationbuttonstate () {raisepropertychanged () => This. cangoback); raisepropertychanged () => This. cangoforward );}}

 

The navigation API can also control the life cycle of a view. When you jump to a page, you can confirm the interception (confirming or cancelling navigation) and other functions. For details, refer to Developer's Guide to Microsoft prism.

Http://www.cnblogs.com/li-xiao/archive/2011/01/31/1947163.html

Prism can help us develop modular programs, divide programs into independent modules for separate development. Then, when the program runs, the modules are combined to provide various functions for the program. Generally, a module is a set of views and functions. Therefore, a method is required to present these views at a specific time in some form. Prism uses shell + region to organize the layout of views and convert views.

 

 

As shown in, shell is equivalent to the master page in ASP. NET. It defines the layout and topic of the page. The navigation area and content area are reserved parts that need to be filled with content, that is, region, which serves as a placeholder. during runtime, the program dynamically fills the content in region.

So how can we define a region as region?

First, introduce the prism namespace.

Xmlns: prism = "http://www.codeplex.com/prism" If ide cannot find this namespace, You need to register prism first.

Add the attached property to the control to be defined as region.

<ContentcontrolPrism: regionmanager. regionname = "mainregion"/>

Not all controls can be used as region. You need to add a regionadapter for the control to be defined as region. Regionadapter is used to create the corresponding region for a specific control, bind the control to the region, and then add some actions for the region. A regionadapter needs to implement the iregionadapter interface. If you need to customize a regionadapter, you can save some work by inheriting the regionadapterbase class. Prism provides several regionadapters for Silverlight:

    • Contentcontrolregionadapter: Creates a singleactiveregion and binds it to contentcontrol.
    • Itemscontrolregionadapter: creates an allactiveregion and binds it to itemscontrol.
    • Selectorregionadapter: Creates a region and binds it to the selector.
    • Tabcontrolregionadapter: Creates a region and binds it to tabcontrol.

As you can see, the four views in the navigationregion corresponding to the navigation area are highlighted, while only one of the four views in the contentregion corresponding to the content area is highlighted (orange indicates that the views are displayed on the page ). Itemscontrol is originally composed of many items. Therefore, itemscontrolregionadapter creates allactiveregion. All active views of this type of region are displayed in itemscontrol, while contentcontrol can only accommodate one content, therefore, contentcontrolregionadapter creates a singleactiveregion. Only one of the Views is active and will be displayed in contentcontrol. Others are invisible and need to be activated (active ), to display it.

Generally, we do not directly deal with region, but through regionmanager, which implements the iregionmanager interface. The iregionmanager Interface contains a read-only attribute regions, which is a collection of region and a createregionmanager method. Prism uses the regionmanagerextensions class to add more features to iregionmanager using the extension method.

    • Addtoregion: Add a view to a region.
    • Registerviewwithregion: associate a view with a region. When region is displayed, the associated view is displayed. That is to say, the associated view is not created before this region is displayed.
    • Requestnavigate: Performs page switching and switches the view displayed in the specified region to the specified view.

As mentioned at the beginning of this article, you need to display the views scattered across modules at specific locations on the page at runtime. First, you need to define the display area of the page, that is, region. Then define the time and method for creating a view. There are two ways to define the ing relationship between views and region in Prism: View discovery and view injection.

View discovery establish the relationship between region and view in declarative mode. For example, in the navigation area, you need to fill in the navigation views when they are displayed in the navigation area. A default content view is also required in the content area. You can also understand view discovery in this way, that is, to specify a default view of region. We can use the iregionmanager. registerviewwithregion method to declare which view a region should display by default. Note that this is register, which is registration, that is, the view will not be created immediately. When region is displayed on the page, it searches for the view associated with itself and initializes it.

The advantage of this is that we don't have to worry about when to create a view, and everything will be done automatically. The disadvantage is that the default view is determined. This method does not work when views need to be converted. In this case, view injection is required.

View injection gives us more precise control over the views displayed in region. Generally, you can add a view to a region by calling the iregionmanager. addtoregion method or the iregionmanager. Regions ["regionname"]. Add method.Instance. For singleactiveregion (contentcontrolregionadapter creates this type of region), you can use the iregion. Activate method to display a view that has been added to region. Of course, you can also use the iregion. Deactivate method to set the view status to non-active or simply call the iregion. Remove Method to remove the view. As you can see, because you want to add a view instance, You need to carefully design when to use view injection to avoid unnecessary overhead.

Some navigation APIs are added in Prism 4.0. This API greatly simplifies the view injection process. It uses URI to navigate the View Graph in region, and then creates a view based on Uri, add it to region, and then activate the view. The navigation API is not only used to simplify the view injection process, but also provides forward and backward functions and provides good support for navigation in mvvm mode, parameters can also be passed during navigation. Therefore, we recommend that you use the new navigation API, that is, the iregionmanager. requestnavigate method.

If a page is relatively unchanged, such as the navigation area, it will not be changed easily after the initialization process of the program. At this time, it is more suitable to use the registerviewwithregion method, this process can usually be completed in the module initialize method.

Public void initialize () {logger. log ("initialize the navigation module", category. debug, priority. low); _ regionmanager. registerviewwithregion (regionnames. navregion, typeof (navigationitem); _ regionmanager. registerviewwithregion (regionnames. mainregion, // both methods can be () => _ container. resolve <navigationcontainer> (); _ regionmanager. registerviewwithregion (regionnames. navdemoactionregion, typeof (actioncontroller ));}

If you need to switch pages frequently in a region, such as the main content area, you can use view injection.

Iregionmanager regionmanager =...; iregion mainregion = regionmanager. Regions ["mainregion"]; inboxview view = This. Container. Resolve <inboxview> (); mainregion. Add (View );

We can see that the view instance has been generated at this time. As mentioned before, a region can contain multiple views which are in different States. For region of itemscontrol type, many items are displayed, therefore, you can add it. However, for region such as contentcontrol, only one view can be displayed at a time. Therefore, an activate process is required after adding it.

To use URI for navigation, you only need to provide the name of the view to be switched. You do not need to know the type of the view to achieve decoupling, and you can use URI to transmit parameters.

 
Public void initialize () {// because prism cannot determine the type of each view, the object is used. Therefore, iservicelocator is used when obtaining an instance based on viewname. getinstance <Object> (viewname)
_ Container. registertype <object, viewa> (viewnames. viewa); _ container. registertype <object, viewb> (viewnames. viewb); _ container. registertype <object, viewc> (viewnames. viewc );}

First, register the view type to associate the view name with the view type. You can call the requestnavigate method during navigation.

 
Void tospecifiedview (string viewname) {URI uri = new uri (viewname, urikind. relative); _ regionmanager. requestnavigate (regionnames. navdemoshowregion, Uri); logger. log ("Jump to view [" + viewname + "]", category. info, priority. low );}

Prism provides the uriquery class to help us PASS Parameters during navigation.

Void tospecifiedview (string viewname) {uriquery query = new uriquery (); If (viewname = viewnames. viewa) {query. add ("time", datetime. now. tow.timestring ();} uri = new uri (viewname + query. tostring (), urikind. relative); _ regionmanager. requestnavigate (regionnames. navdemoshowregion, Uri, callbackhandler); // you can add or remove the callback method}

The code above determines that a parameter called time is passed when a jump is made to viewa. So how can we get the passed parameters in the view? Here we will mention the inavigationaware interface. This interface allows the view or its corresponding viewmodel to participate in the page navigation process. Therefore, this interface can be implemented either by the view or by the datacontext of the view, which usually refers to the viewmodel.

Public interface inavigationaware {bool isnavigationtarget (navigationcontext); void onnavigatedto (navigationcontext); void onnavigatedfrom (navigationcontext );}

When you go from this page to another page, the onnavigatedfrom method is called, and the navigationcontext contains the URI of the target page.

When navigating from other pages to this page, isnavigationtarget is called first, and isnavigationtarget returns a bool value. Simply put, this method is used to tell prism, whether to reuse this view or create another instance. Call the onnavigatedto method. When you navigate to this page, you can retrieve the passed parameters from navigationcontext.

Another advantage of using the navigation API is that you can move the page forward and backward, and everything is done by prism. This function is provided by the iregionnavigationjournal interface.

Public interface iregionnavigationjournal {bool cangoback {Get;} bool cangoforward {Get;} implements currententry {Get;} inavigateasync navigationtarget {Get; set;} void clear (); void Goback (); void goforward (); void recordnavigation (iregionnavigationjournalentry entry );}

The cangoback and cangoforward attributes indicate whether the current status can be backward or forward. If possible, you can use the Goback and goforward methods to forward and backward.

Public class actioncontrollerviewmodel: icationicationobject {private iregion _ demoshowregion; Public bool cangoback {get {return _ demoshowregion. navigationservice. journal. cangoback ;}} public bool cangoforward {get {return _ demoshowregion. navigationservice. journal. cangoforward;} void toprevious () {_ demoshowregion. navigationservice. journal. goback (); resetnavigationbuttonstate ();} void tonext () {_ demoshowregion. navigationservice. journal. goforward (); resetnavigationbuttonstate ();} void resetnavigationbuttonstate () {raisepropertychanged () => This. cangoback); raisepropertychanged () => This. cangoforward );}}

 

The navigation API can also control the life cycle of a view. When you jump to a page, you can confirm the interception (confirming or cancelling navigation) and other functions. For details, refer to Developer's Guide to Microsoft prism.

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: info-contact@alibabacloud.com 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.