WP7 does not support multitasking, but provides a tombstone mechanism. In general, it provides five graves for you. If you want your program to enter the "background ", then you will be buried there. If you want to enable your "background" program, you will dig out the program from the grave and load the relevant data into the control from the memory, in simple terms, it is a zombie.
However, if you have more than five "backend" programs, you will be embarrassed to enter the one lying in the grave first. I want to dig you out and let the new guy lie down. In this case, you can't even find the dead. The moment the body disappears, the soul in the memory disappears.
This mechanism involves the application lifecycle. There are a lot of questions on the Internet. There is nothing to discuss about. To put it simply, the program starts launch at the beginning. Then, if you click the START key, the program enters deactivate and the program enters "background". If you click the return key again, the program can be restored from "background. Is activate. Click the return key multiple times to return to the desktop. The program exits. Is closing. These things are defined in APP. XAML.
Another key is that the return program from the "background" can only press the return key. If the program is started from the initiator, the thread of the previous program will exit. That is, you re-opened a new program, which is very unpleasant. If my program is in the fifth place, there are only two ways to start the program from the "background". One is to press the return key and choose to enter the background program, the other is the crazy way to press the return key. After the first four "background" programs are exited, they are returned to the fifth program.
So what makes me very uncomfortable is these two points. One is that there are only five "background programs", and the other is that they cannot enter the background from the initiator.
Then, we only need to simulate the implementation. In deactivate, data is stored in isolatedstorage.
The following is a program with two pages. It simulates entering the "background" on the second page, so it only restores data on the second page.
First, add a page called page2.xaml. Put a textbox named page2msg on it,
Then, put a button in mainpage. XAML to navigate to page2.xaml and add the click event:
private void Button_Click(object sender, RoutedEventArgs e) { NavigationService.Navigate(new Uri("/TombStoneDemo;component/Page2.xaml",UriKind.Relative)); }
The next step is the focus. I save the current page to phoneapplicationservice. And save the textbox content on page2 to phoneapplicationservice. Defines an isolatedstoragesettings with the key as "recovery" and a Boolean value to save whether to restore the interrupted data.
The specific implementation is as follows:
Implemented in the loaded event of mainpage:
Private void phoneapplicationpage_loaded (Object sender, routedeventargs e) {isolatedstoragesetiss ISS = isolatedstorageset.pdf. applicationsettings; bool recov; If (ISS. trygetvalue ("recovery", out recov) {If (recov) // whether to restore, the recovery is directed to page2 {If (phoneapplicationservice. current. state. containskey ("currentpage") {string temp = convert. tostring (phoneapplicationservice. current. state ["currentpage"]); If (! Temp. equals ("mainpage. XAML ") & temp. length> 0) {navigationservice. navigate (New uri ("/tombstonedemo; component/" + temp, urikind. relative); // navigate to currentpage. That is, the previous interrupt page }}else phoneapplicationservice. current. state ["currentpage"] = "mainpage. XAML ";} else phoneapplicationservice. current. state ["currentpage"] = "mainpage. XAML ";}
Add code in page2:
Private void phoneapplicationpage_loaded (Object sender, routedeventargs e) {debug. writeline ("page2 loaded"); isolatedstorageset=iss = isolatedstorageset.pdf. applicationsettings; bool recov; If (ISS. trygetvalue ("recovery", out recov) {If (phoneapplicationservice. current. state. containskey ("currentpage") {string temp = convert. tostring (phoneapplicationservice. current. state ["currentpage"]); If (temp. equals ("page2.xaml") {If (phoneapplicationservice. current. state. containskey ("page2txmsg") {txmsg. TEXT = (string) phoneapplicationservice. current. state ["page2txmsg"]; ISS ["recovery"] = false; // set this attribute to false after restoration, that is, the phoneapplicationservice is restored. current. state ["currentpage"] = "page2.xaml ";}
protected override void OnNavigatedFrom(System.Windows.Navigation.NavigationEventArgs e) { base.OnNavigatedFrom(e);
// Save the textbox value phoneapplicationservice. Current. State ["page2txmsg"] = txmsg. Text;} When you leave page2 ;}
All tombstone users know that this phoneapplicationservice is only stored in the memory. If you start the program from the initiator, the content in the original memory will also be destroyed. Therefore, I want to save the data to isolatedstorage when the program is interrupted.
Add code in launching, deactivate, and closing in APP. XAML:
Isolatedstoragesetpoliciss = isolatedstorageset.pdf. applicationsettings; private void application_launching (Object sender, launchingeventargs e) {// when the program starts, it reads data from the storage string temp; If (ISS. trygetvalue ("lastpage", out temp) {phoneapplicationservice. current. state ["currentpage"] = temp;} If (ISS. trygetvalue ("page2txmsg", out temp) {phoneapplicationservice. current. state ["page2txmsg"] = temp;} p Rivate void application_deactivated (Object sender, deactivatedeventargs e) {// save data to isolatedstorage when the program is interrupted. ISS ["lastpage"] = phoneapplicationservice. current. state ["currentpage"]; ISS ["recovery"] = true; ISS ["page2txmsg"] = phoneapplicationservice. current. state ["page2txmsg"];} private void application_closing (Object sender, closingeventargs e) {ISS ["recovery"] = false ;}
In this way, you can. Enter any data in page2 to the textbox. Click Start. Now, no matter whether you enter the program from the return key or start the program from the initiator, the page2 interface that we have not edited is returned. In this way, the multi-task operation is simulated.
Conclusion: now we can simulate multi-task operations. The problem is that there are too many controls. Too many pages, so it is quite tedious to write.