Region of Prism (2)

Source: Internet
Author: User

In Region (1) of prism, we introduce page organization and switching methods in prism. This article will take a very simple exampleProgramTo practice.

Source code download

Below is:

First, let's talk about log. Prism has built-in ilogfacade interface. In modularity of the Quickstart project provided by Prism, there is a callbacklogger, which we can use directly here. Then, put a textbox in the prismregionshell to display the log Content in the textbox. It is worth mentioning that in order to directly display the new log output, the scroll bar of the textbox needs to scroll to the bottom. The following method is used:

 
Public void log (string message, category, priority) {This. logcontainer. text + = string. format (cultureinfo. currentuiculture, "[{0}] [{1}] {2} \ r \ n", category, priority, message ); //CodeThe function is to scroll the scroll bar of the text box to the bottom of logcontainer. Select (logcontainer. Text. length, logcontainer. Text. Length );}

Then let's talk about the navigation area on the left. Here, an itemscontrol is placed and set it to region. The two buttons are not directly written to the XAML, but are dynamically added during initialization of the two modules. That is to say, the itemscontrol does not know which items it will contain. Here we use iregionmanager. registerviewwithregion (regionnames. navregion, typeof (emptynavigationitem); in this way, the view is registered to region. In this way, the two views are initialized only when the region is displayed. Note the order of views when multiple views need to be displayed at the same time in a region. For example, itemscontrol, which is first registered and displayed on the top, but because of the module loading speed and other reasons, the two views are not necessarily on the top. Now I need to specify the [navigation example] button, so prism provides us with viewsorthintattribute to solve this problem. Add the corresponding attribute to the view to be sorted.

 
[Viewsorthint ("01")] public partial class navigationitem: usercontrol [viewsorthint ("02")] public partial class emptynavigationitem: usercontrol

When initializing the module of the navigation instance, register the view of the navigation example to the region of the content area. At this time, the [previous] button is still in the gray state, because the page displayed by registerviewwithregion method is not recorded. When you click the three buttons [viewa] [viewb] [viewc], the requestnavigate method is used to redirect the page. The page Jump process is recorded, in this case, you can use the [previous] and [next] buttons to forward and backward the page.

 
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); logger. log ("Jump to view [" + viewname + "]", category. info, priority. low); resetnavigationbuttonstate ();}

Note that the three views have been registered using the iunitycontainer. registertype <object, viewa> (viewnames. viewa) method when the module is initialized.

 Public void initialize () {logger. log ("initialize the navigation module", category. debug, priority. low); _ regionmanager. registerviewwithregion (regionnames. navregion, typeof (navigationitem); _ regionmanager. registerviewwithregion (regionnames. mainregion, () => _ container. resolve 
  
    (); _ regionmanager. registerviewwithregion (regionnames. navdemoactionregion, typeof (actioncontroller); // note that the registered type must be object. Because prism cannot determine the view type, object _ container is used. registertype 
   
     (viewnames. viewa); _ container. registertype 
    
      (viewnames. viewb); _ container. registertype 
     
       (viewnames. viewc) ;}
     
    
   
  

Both viewa and viewb implement the inavigationaware interface. The difference is that viewa is implemented in its corresponding viewmodel viewaviewmodel class, while viewb is implemented directly in code behind. Prism provides good support for mvvm, so you can choose to implement this interface in the view or in the corresponding viewmodel.

 
Public bool isnavigationtarget (navigationcontext) {return false ;}

In viewb, The isnavigationtarget method returns false, while viewa returns true. You can click the three buttons to jump to the page. Observe the log and find that viewa is created only once, while viewb is created again every time. In addition, parameters are passed during the jump to viewa. You can retrieve the parameters in the onnavigatedto method.

Public void onnavigatedto (navigationcontext) {uriquery query = navigationcontext. parameters; string time = Query ["time"]; logger. log (string. format ("viewa: current time {0}", time), category. info, priority. medium );}

Source code download

In Region (1) of prism, we introduce page organization and switching methods in prism. This article will be used as a simple example program.

Source code download

Below is:

First, let's talk about log. Prism has built-in ilogfacade interface. In modularity of the Quickstart project provided by Prism, there is a callbacklogger, which we can use directly here. Then, put a textbox in the prismregionshell to display the log Content in the textbox. It is worth mentioning that in order to directly display the new log output, the scroll bar of the textbox needs to scroll to the bottom. The following method is used:

Public void log (string message, category, priority) {This. logcontainer. text + = string. format (cultureinfo. currentuiculture, "[{0}] [{1}] {2} \ r \ n", category, priority, message ); // This code is used to scroll the scroll bar of the text box to the bottom of logcontainer. select (logcontainer. text. length, logcontainer. text. length );}

Then let's talk about the navigation area on the left. Here, an itemscontrol is placed and set it to region. The two buttons are not directly written to the XAML, but are dynamically added during initialization of the two modules. That is to say, the itemscontrol does not know which items it will contain. Here we use iregionmanager. registerviewwithregion (regionnames. navregion, typeof (emptynavigationitem); in this way, the view is registered to region. In this way, the two views are initialized only when the region is displayed. Note the order of views when multiple views need to be displayed at the same time in a region. For example, itemscontrol, which is first registered and displayed on the top, but because of the module loading speed and other reasons, the two views are not necessarily on the top. Now I need to specify the [navigation example] button, so prism provides us with viewsorthintattribute to solve this problem. Add the corresponding attribute to the view to be sorted.

[Viewsorthint ("01")] public partial class navigationitem: usercontrol [viewsorthint ("02")] public partial class emptynavigationitem: usercontrol

When initializing the module of the navigation instance, register the view of the navigation example to the region of the content area. At this time, the [previous] button is still in the gray state, because the page displayed by registerviewwithregion method is not recorded. When you click the three buttons [viewa] [viewb] [viewc], the requestnavigate method is used to redirect the page. The page Jump process is recorded, in this case, you can use the [previous] and [next] buttons to forward and backward the page.

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); logger. log ("Jump to view [" + viewname + "]", category. info, priority. low); resetnavigationbuttonstate ();}

Note that the three views have been registered using the iunitycontainer. registertype <object, viewa> (viewnames. viewa) method when the module is initialized.

 Public void initialize () {logger. log ("initialize the navigation module", category. debug, priority. low); _ regionmanager. registerviewwithregion (regionnames. navregion, typeof (navigationitem); _ regionmanager. registerviewwithregion (regionnames. mainregion, () => _ container. resolve 
  
    (); _ regionmanager. registerviewwithregion (regionnames. navdemoactionregion, typeof (actioncontroller); // note that the registered type must be object. Because prism cannot determine the view type, object _ container is used. registertype 
   
     (viewnames. viewa); _ container. registertype 
    
      (viewnames. viewb); _ container. registertype 
     
       (viewnames. viewc) ;}
     
    
   
  

Both viewa and viewb implement the inavigationaware interface. The difference is that viewa is implemented in its corresponding viewmodel viewaviewmodel class, while viewb is implemented directly in code behind. Prism provides good support for mvvm, so you can choose to implement this interface in the view or in the corresponding viewmodel.

 
Public bool isnavigationtarget (navigationcontext) {return false ;}

In viewb, The isnavigationtarget method returns false, while viewa returns true. You can click the three buttons to jump to the page. Observe the log and find that viewa is created only once, while viewb is created again every time. In addition, parameters are passed during the jump to viewa. You can retrieve the parameters in the onnavigatedto method.

Public void onnavigatedto (navigationcontext) {uriquery query = navigationcontext. parameters; string time = Query ["time"]; logger. log (string. format ("viewa: current time {0}", time), category. info, priority. medium );}

Source code download

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.