Windows phone tombstone (9)

Source: Internet
Author: User

650) this. width = 650; "height =" 45 "border =" 0 "width =" 600 "src =" http://www.bkjia.com/uploads/allimg/131228/0TJ3K62-0.jpg "alt =" "/>

Several terms:

 

Name

Description

Launched

Start a new program

Closed

Use the Back button to close the program

Deactivated

A program is running. Press the start key to stop the program. Then, the program enters the tombstone.

Activated

The program is revived from the tombstone, that is, activated

Then the life cycle of our program is roughly shown:


 

650) this. width = 650; "height =" 340 "width =" 454 "src =" http://www.bkjia.com/uploads/allimg/131228/0TJ35c5-1.jpg "alt =" "/>

Life Cycle Description: When we run program A on the start page, program A enters the Launched startup stage, the program is generally in the running state for a long time. If you press the Back key at this time, the program enters the Closed state directly, and there is an emergency during the running process, such as calling and sending information, or you need to run program B, you press the Start key, and press the Start key in the Deactivated state, and then in the process of running program B, program A is always in the Tombstoning status (unless you re-enable the new instance of program A from the Start page). If you press the Back key during the process of operating program B, the system calls out the card-type task manager, which contains the program A, program B, and Start pages. If you click program A, program A is activated and enters the running status, when the program is closed, the life cycle of program A is completed;

In the App. xaml. cs file, you can find the events triggered when these statuses are changed. The annotations in the events are not described in detail.

650) this. width = 650; "style =" "id =" code_img_opened_56bbca2f-3165-407f-9a04-b5b644c4fd20 "class =" code_img_opened "src =" http://www.bkjia.com/uploads/allimg/131228/0TJ32501-2.gif "alt =" "/> View Code // application startup (for example, code executed when starting from the "Start" menu)
// This code is not executed when the application is re-activated
Private void Application_Launching (object sender, LaunchingEventArgs e)
{
// You can read data from an independent bucket for initialization.
}

// Code executed when the application is activated (placed on the frontend)
// This code is not executed when the application is started for the first time
Private void Application_Activated (object sender, ActivatedEventArgs e)
{
// Temporary data can be restored


}

// Stop the code executed when the application (sent to the background)
// This code is not executed when the application is closed
Private void Application_Deactivated (object sender, DeactivatedEventArgs e)
{
// Store temporary data so that the user can activate the data from the tombstone status

}

// Code executed when the application is closed (for example, when the user clicks back)
// This code is not executed when the application is disabled
Private void Application_Closing (object sender, ClosingEventArgs e)
{
// When the program presses the Back key, it can be stored to an independent Bucket
} Copy the code

Effect:

650) this. width = 650; "height =" 320 "border =" 0 "width =" 330 "src =" http://www.bkjia.com/uploads/allimg/131228/0TJ31561-3.png "alt =" "/>

The following example shows how to temporarily store data after the user closes the program, reactivate it from the task manager, or return it to the program through the Back key; in fact, the user does not feel that the program has been closed. When the user program returns to the program, it is consistent with what the user showed when the user leaves;

The MainPage. xaml page is not modified, so it is not pasted. The MainPage hides the file code:

650) this. width = 650; "style =" display: inline; "id =" code_img_opened_5647609a-be1d-496c-af85-97ec220ddba7 "class =" code_img_opened "src =" http://www.bkjia.com/uploads/allimg/131228/0TJ32501-2.gif "alt =" "/> View Code using System;
Using System. Collections. Generic;
Using System. Linq;
Using System. Net;
Using System. Windows;
Using System. Windows. Controls;
Using System. Windows. Documents;
Using System. Windows. Input;
Using System. Windows. Media;
Using System. Windows. Media. Animation;
Using System. Windows. Shapes;
Using Microsoft. Phone. Controls;
//
Using Microsoft. Phone. Shell;

Namespace Tombstoned
{
Public partial class MainPage: PhoneApplicationPage
{
Int sum = 0;
App ap = (App) Application. Current;
// Constructor
Public MainPage ()
{
InitializeComponent ();
}
// It is verified that this method is loaded only when the program is started.
Private void PhoneApplicationPage_Loaded (object sender, RoutedEventArgs e)
{
// This. ApplicationTitle. Text = ap. appKey;
}
Protected override void OnManipulationStarted (ManipulationStartedEventArgs e)
{
Sum ++;
// Display the program title
This. PageTitle. Text = sum. ToString ();
Ap. appKey = this. PageTitle. Text;
Base. OnManipulationStarted (e );
}

Protected override void OnNavigatedTo (System. Windows. Navigation. NavigationEventArgs e)
{
If (! String. IsNullOrEmpty (ap. appKey ))
{
This. PageTitle. Text = ap. appKey;
}

Base. OnNavigatedTo (e );
}

}
} Copy the code

From the code above, we can see that we saved the data to a public attribute in the App class. In the touch event, we assigned the value of the Self-added value to the appKey of the App public attribute, after loading the MainPage, call the OnNavigatedTo Method for initialization. We can see how the App class implements the temporary data stored in the App class; the code added to the App hidden file is as follows:

Public string appKey {get; set ;}

That is, to save the public attributes of data, we use two methods and implement them:

650) this. length = 650; "style =" "id =" code_img_opened_0f98b2a5-ff86-4f7d-a746-2d8bb902c754 "class =" code_img_opened "src =" http://www.bkjia.com/uploads/allimg/131228/0TJ32501-2.gif "alt =" "/> View Code // activate the application (front-end) code executed
// This code is not executed when the application is started for the first time
Private void Application_Activated (object sender, ActivatedEventArgs e)
{
// Temporary data can be restored
Object str = string. Empty;

If (PhoneApplicationService. Current. State. TryGetValue ("key", out str ))
{
AppKey = str. ToString ();
}

}

// Stop the code executed when the application (sent to the background)
// This code is not executed when the application is closed
Private void Application_Deactivated (object sender, DeactivatedEventArgs e)
{
// Store temporary data so that the user can activate the data from the tombstone status
PhoneApplicationService. Current. State ["key"] = appKey;
} Copy the code

We can see that we use it when users encounter emergencies, such as calls.PhoneApplicationService. Current. State is stored, and the modified program is activated when the user uses the task manager or the Back key, and

The PhoneApplicationService. Current. State. TryGetValue method obtains the value. The advantage of this method is that no exception information is reported.; Task Manager's:

 

650) this. width = 650; "height =" 553 "border =" 0 "width =" 332 "src =" http://www.bkjia.com/uploads/allimg/131228/0TJ3FI-6.png "alt =" "/>

Windows phone 7 manages multiple applications: windows phone 7 manages multiple applications through stacks. When we run program A, if we need to run program B, then we press the Start key, and the Start key will press program A into the stack. At this time, program A stops running. If you need to run program C, similarly, program B will be pushed into the stack. Then, press the Back key to terminate the current program C and obtain the last program B from the stack for activation;

This article from the "Shenzhou Dragon" blog, please be sure to keep this source http://shenzhoulong.blog.51cto.com/1191789/834788

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.