Note: This article is intended for the runtime app.
After creating a project, you will find the following code in the app class:
// Todo: change this value to the cache size of your application rootframe. cachesize = 1;
This line of code is put in the onlaunched method, and cachesize is a public attribute of the frame class.
We know that the frame class is responsible for completing the navigation between pages. It is a container control. The cachesize attribute is used to tell the frame object the number of pages to be cached. Recently, someone asked whether the number of cached pages is the number of navigation records?
Clearly tell you: neither.
The navigation of the frame is recorded in the backstack attribute,This cache is not used to cache page navigation records. Be sure not to confuse it..
The cachesize attribute is used to setNumber of page instances to be retained. When a frame is redirected to a page, the constructor of the page is called to create a new instance of a page. That is, no matter whether you navigate forward or backward, If you navigate to a page, a page will be re-instantiated. Therefore, we can understand why the navigate method of the frame class for navigation should be defined as follows:
public bool Navigate(System.Type sourcePageType)
This is different from the navigation in Silverlight. In SL, Uri is passed, and the type of the page class is passed here, because the type of a page class is obtained, in the navigation bar is a new instance.
Is it abstract? It's okay. The example is an invincible learning tool. Do not hesitate.
1. Start Vs and create a "Windows Phone" project. It is said that Ms will unify the names of all platforms. Of course this is required and Gates dream. No matter what the name is, the system is the same, but the name is different.
2. In the app class, find the onlaunch method and change the cahesize to 0.
rootFrame.CacheSize = 0;
3. Delete the default mainpage. XAML generated by the project because it exists and seriously affects the demo. Why? I will tell you later.
4. Add three blank pages to the project. will this happen? If not, press Ctrl + Shift + A to try.
5. The structure of the three pages is similar, so here I only talk about the first page. The XAML code is as follows:
<Grid> <grid. rowdefinitions> <rowdefinition Height = "Auto"/> <rowdefinition/> </grid. rowdefinitions> <textblock grid. row = "0" text = "Page 1" style = "{staticresource headertextblockstyle}"/> <stackpanel grid. row = "1"> <textblock text = "Page initialization" fontsize = "45" margin = ","> <textblock. foreground> <solidcolorbrush X: Name = "SLB" color = "white"/> </textblock. foreground> </textblock> <button content = "next page" grid. row = "1" Click = "onnext"/> </stackpanel> </GRID> <page. resources> <storyboard X: Key = "STD" repeatbehavior = "5x"> <coloranimation duration = "0: 0. 5 "storyboard. targetname = "SLB" storyboard. targetproperty = "color" from = "Purple" to = "yellow"/> </storyboard> </page. resources>
In the page resource list, a storyboard is declared, and the text color of the textblock that displays "Page initialization" is changed with an animation.
The idea is as follows:Play the animation in the page class constructor, And the animation is repeatedly played for five times (specified by repeatbehavior ). If the page is instantiated, the constructor is also called and the animation is played. When navigating between pages, you can know whether the constructor is called by watching whether the color-changing animation on the page will be played, so as to know whether a new instance is generated on the page.
6. In the page class constructor, retrieve the storyboard object in the resource and start playing the animation.
Public page1 () {This. initializecomponent (); navigationcachemode = windows. UI. XAML. navigation. navigationcachemode. required; // If the constructor is called, play the animation storyboard STD = This. resources ["STD"] As storyboard; STD. begin ();}
In the above Code, there is another sentence:
NavigationCacheMode = Windows.UI.Xaml.Navigation.NavigationCacheMode.Required;
The page class has a navigationcachemode attribute, which must be set to Enabled or required. Do not set it to disabled. Because disabled indicates that the page cache is disabled. if disabled, it is useless even if the cachesize is set to 1 million, and the page instances will not be cached.
What is the difference between enabled and required? The difference is whether to force cache. If it is set to enabled and does not exceed the number of page caches specified by cachesize, the page instance will be cached. However, once the number of cached pages exceeds the number specified by cachesize, even if you set the page cache mode to enabled, the page instance will not be cached.
The required mode is different. Even if the total number of cached page instances exceeds the cachesize value, the pages with the required mode set will still be forcibly cached.
However, please note that at any time, do not set cachesize to 0. If it is 0, no cache will occur even if the page uses the required mode..
That is to say, always make the cachesize value greater than 0.
In this example, there are three pages. Set cachesize to 3.
rootFrame.CacheSize = 3;
With page cache, You can temporarily Save the page class instance while the application is running. You do not need to instantiate the page every time you navigate to the page, saving performance overhead, this is especially useful when some pages are time-consuming during initialization. They are only instantiated during the first visit and will not be instantiated when the next visit is made. This will increase the response speed of the program.
The onnavigatedto method is called no matter whether the page instance is cached or not. However, if you switch to another application or return to the Start Screen, and the application is suspended (interrupted), the page will not be re-instantiated and the onnavigatedto method will not be called.
After an application is suspended, the app's suspending event is triggered. When the application returns to the application again, the app's resuming event is triggered.
Note that this is different from the application of the WordPress previous Silverlight framework. A navigation event is triggered when the SL page leaves or returns to the application, the APIS transplanted from the RT library are consistent with those transplanted from the win 8x application. Leaving or returning to the current application will not cause navigation events on the page.
The example mentioned in this article is: http://files.cnblogs.com/tcjiaan/%E9%A1%B5%E9%9D%A2%E7%BC%93%E5%AD%98%E7%A4%BA%E4%BE%8B.rar
[WP Development] Correct Understanding of page Cache