Windows 8 Metro development FAQ (1) -- navigation

Source: Internet
Author: User

Win8 navigation really makes me a little depressed, especially when I did WP7 development, it was really difficult to adapt.

There are currently two problems with Win8 Navigation:

    1. If the page cache is not enabled, the page will be refreshed every time the page is returned (all the statuses on the page are automatically saved in WP7, and the returned status is the same when the page is left ), as a result, the user status needs to be saved by the developer. You may say that in this case, you can start the page cache. Yes, you can enable the page cache, but once the page cache is enabled, you can only have one instance at a time on this page, in this way, it is very difficult to display pages with different parameters, because you only have one instance at a time.
    2. Users can pass object parameters during navigation. I was so happy when I saw that the object parameters could be passed. I felt that Microsoft finally improved the WP7 navigation model, however, when debugging is suspended, I find that I am wrong and crash directly. So I asked you at the official Microsoft website. The answer is: if the parameter passed is of a complex type (even if you have marked datacontract and datamember ),ProgramThe crash will continue to occur when it is suspended. That is to say, you must serialize your complex type into a string before it is passed, and then deserialize the string into a corresponding object on the target page. I think Microsoft, are you doing this interesting? Nima, you might as well use the WP7 navigation model!

 

The first question is discussed below. First, if your page does not need to display different data based on different parameters, you can enable page caching (the navigationcachemode of the page is set to Enabled or required ). If your page needs to display different data based on different parameters, you need to do more things as a developer than the WP7 navigation model. The first step is to save the page data, the second is to save the Page Status (if there is a scroll bar, you have to save the status of the scroll bar, if there is a text box, you have to save the text in the text box .....). Now, use the built-in template in vs2012 to create a column.

 

Select the grid app template from the Project template, and run it directly after the project is created. Go to the home page, slide the scroll bar, slide the last part, and select a specific item to enter the details page. Then, return to the details page. Then, you will find that the page returns to the initial status and the scroll bar also returns to the origin. Now I want to explain why.

First, the page does not enable cache, so the page will always be refreshed every time it is returned. How can I refresh it? In fact, the system re-calls the initializecomponent method when the returned result. All data and status no longer exist, just like a brand new page (you should regard it as a brand new page ).

If this is the only option, we will be crazy. Fortunately, Microsoft has left two methods, loadstate and savestate. In fact, these two methods are the custom methods in the Project template.

    • Loadstate method. When the page is loaded for the first time (note that it is not returned), The pagestate parameter is null. When the page is loaded when the previous page is returned, the pagestate parameter contains the data you saved in the savestate method. Then you can restore the data and status of the page.
    • The savestate method. When you navigate from the current page to another page, the savesate method is called on the current page. At this time, you need to save the page data and status to pagestate, use the stored data for loadstate.

Taking groupeditemspage as an example, we need to add someCode:

  Protected   Override   Void Savestate (Dictionary <String , Object > Pagestate ){  Base  . Savestate (pagestate );  //  Transfers all data in defaultviewmodel to pagestate.              Foreach ( VaR Item In   This  . Defaultviewmodel) {pagestate [item. Key] = Item. value ;} //  Save the scroll status of the scroll bar. Only the horizontaloffset is saved here.              VaR Scroll = itemgridview. getvisualdescendants <scrollviewer> (). Firstordefault (); pagestate [  "  Horizontaloffset  " ] = Scroll = Null ? 0  : Scroll. horizontaloffset ;} 

Then add the following code in loadstate:

   Protected   Override   Void Loadstate (Object navigationparameter, Dictionary <string, Object> Pagestate ){  //  If pagestate is not null, it indicates that the status is returned. At this time, data and status need to be restored.              If (Pagestate! = Null  ) {Itemgridview. layoutupdated + = Itemgridview_layoutupdated;  If (Pagestate! = Null  ){  Foreach (VaR Item In  Pagestate ){  This . Defaultviewmodel [item. Key] = Item. Value ;}}}  Else  {  //  Pagestate is null, indicating that the page is loaded for the first time and data needs to be initialized.                  VaR Sampledatagroups = Sampledatasource. getgroups (string) navigationparameter );  This . Defaultviewmodel [ "  Groups  " ] = Sampledatagroups ;}} 

The following describes how to add the itemgridview_layoutupdated event:

   //  Page scroll bar  Scrollviewer scroll;  Void Itemgridview_layoutupdated ( Object Sender, Object  E ){  If (Scroll =Null  ) Scroll = Itemgridview. getvisualdescendants <scrollviewer> (). Firstordefault ();  If (Scroll! = Null  ){  If ( This . Defaultviewmodel. containskey ( "  Horizontaloffset  "  )){  Double D = ( Double ) This . Defaultviewmodel [ "  Horizontaloffset  "  ]; Scroll. scrolltohorizontaloffset (d );  //  The status of the scroll bar cannot be restored immediately. You may need to call scrolltohorizontaloffset multiple times to restore it accurately.                      If (D = Scroll. horizontaloffset) itemgridview. layoutupdated -= Itemgridview_layoutupdated ;} Else  //  If there is no horizontaloffset data in defaultviewmodel, you do not need to restore the scroll bar status. Itemgridview. layoutupdated-= Itemgridview_layoutupdated ;}} 

After adding the code, you can directly run it. Then you will find that both page data and page status can be restored to the last status.

The getvisualdescendants method involved in the above Code is an extension method. The specific code is as follows:

  Public   Static   Class  Visualtreeextensions {  Public   Static Ienumerable <t> getvisualdescendants <t> (This Dependencyobject element) Where  T: dependencyobject {  Return Element. getvisualdescendants (). oftype <t> ();}  ///   <Summary>          ///  Obtains all child nodes of an element.  ///   </Summary>          Public   Static Ienumerable <dependencyobject> getvisualdescendants (This  Dependencyobject element ){  If (Element = Null  ){  Throw   New Argumentnullexception ( "  Element  "  );}  Return Getvisualdescendantsandselfiterator (element). Skip ( 1 );}  Private   Static Ienumerable <dependencyobject> Getvisualdescendantsandselfiterator (dependencyobject element) {queue <Dependencyobject> remaining = New Queue <dependencyobject> (); Remaining. enqueue (element );  While (Remaining. Count> 0  ) {Dependencyobject OBJ = Remaining. dequeue (); Yield   Return  OBJ;  Foreach (Dependencyobject child In  OBJ. getvisualchildren () {remaining. enqueue (child );}}}  Public   Static Ienumerable <dependencyobject> getvisualchildren ( This  Dependencyobject element ){  If (Element = Null ){  Throw   New Argumentnullexception ( "  Element  "  );}  Return Getvisualchildrenandselfiterator (element). Skip ( 1  );}  Private   Static Ienumerable <dependencyobject> getvisualchildrenandselfiterator ( This Dependencyobject element ){  Yield   Return  Element;  Int Count = Visualtreehelper. getchildrencount (element );  For ( Int I = 0 ; I <count; I ++ ){  Yield   Return Visualtreehelper. getchild (element, I );}}} 

 

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: 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.