I mentioned the navigation problem in the first article of this series, but some problems may not be understood yet. So I will write another blog about navigation here.
There are two problems:
1. Why cannot I directly transfer objects other than simple types when calling the frame. navigate method?
2. Why will the page be initialized every time the page is back?
Let me first talk about the vs Project template. When we create a project, if we do not select a blank project, Vs will automatically create a project containing the common folder, each page is inherited from layoutawarepage. layoutawarepage helps us with a lot of necessary operations.CodeYou will find that layoutawarepage overrides the navigateto and navigatefrom methods, and adds two more virtual Methods: loadstate and savestate. In this way, we can almost forget the navigateto and navigatefrom methods, and simply use the loadstate and savestate methods. Here we have to explain why layoutawarepage has rewritten two methods. Looking at the layoutawarepage code, we can find that layoutawarepage performs data recovery in the navigateto method. When the processing is complete, the loadstate method is called, and pass the stored data to the loadstate method. The data here is only available when the back is used, because the data must be restored only when the back is used. Layoutawarepage saves data in the navigatefrom method. When you navigate from the current page to another page, your data will be saved. You need to process the data you want to save.
Then let's take a look at how the project template helps us restore the stored data.
All the data stored by the savestate method is processed by the suspensionmanager class. WhenProgramAt startup, suspensionmanager. registerframe will be called in the onlaunched method of the app class to initialize suspensionmanager. If the program is restored from the suspended state, suspensionmanager. restoreasync will be called to restore data. When the program is suspended, suspensionmanager. saveasync is called in the onsuspending method of the app class to save the data (Data is saved as files.).
The restoration and storage of the data have been finished. Then I want to explain the first question mentioned above, why is frame called. when the navigate method is used, objects other than simple types cannot be directly transmitted?
The problem lies in suspensionmanager. in the saveasync method, because this method will call frame. getnavigationstate method. In this case, if the parameters passed on the page are complex objects, crash is used directly, because frame. getnavigationstate Cannot serialize your object. Even if you have added [datacontract] and [datamenber] tags to your class, it is useless. For this question, I also asked related Microsoft technical staff, the answer to me is to serialize the object into a string and then deserialize it into an object when passing the object. Isn't that a pitfall !!!
Next I will talk about the second question mentioned above. Why does the page initialize every time the page is back?
In my opinion (This is purely a personal opinion, with hypothetical components. You are welcome to shoot bricks.).
I think the reason why the page will be initialized every time you back is largely based on the memory usage. Most of the apps we download from the store display content in images. Because the screen size of a tablet is definitely larger than that of a mobile phone, the number of images displayed at the same time is bound to be large, even if you enable the virtualization technology, this problem still persists. The size of an image is almost the same as that of a novel, and a page with a large number of images will inevitably occupy a large amount of memory, an app has a lot of pages. If every page is like this, the memory occupied by an app will be huge! Now when I navigate from the current page to other pages, I think the system will release the original page to save memory overhead, in this way, your app will remain in a stable State with the data virtualization technology in terms of memory overhead (the memory of the app we are currently developing is basically between 60 and 80 Mb ).
What is data virtualization technology?Data virtualization is exclusive to list controls. If you have 1000 pieces of data as the data source to the list control, if you have not enabled data Virtualization (enabled by default), the list control will load 1000 items, every item occupies memory resources. You can imagine how many resources your app will occupy, not just the memory overhead, there is also a card phenomenon when you bind the data source (because the list control needs to initialize 1000 items at once, it's strange not to get stuck !). Fortunately, data virtualization is enabled for the current gridview, listview, ListBox, and flipview by default, so that if you have 1000 pieces of data, the system only needs to initialize 10 or fewer items (the number of System Computing items to be initialized), which reduces the memory overhead and accelerates the initialization of list controls.
Data virtualization relies on the itemspanel container of the list control. The default containers of these list controls are inherited from the virtualizingpanel, both Win8 wrapgrid and virtualizingstackpanel support data virtualization containers. We can also customize containers that support virtualization, but this process is very complicated.
Currently, I have used a social app. I can confirm that this app does not use virtualization technology. It has the paging function, when I continue to load the page, the memory usage is rising. When I browsed about 100 pages, the memory usage is already 1 GB. You are not mistaken, it is 1 GB.
This article is here!Coming soon next
Windows 8 difficult to develop (4) -- Database