Before proceeding with WP development, like other development technologies, we need to have a brief understanding of the lifecycle of a WP application sequence. We do not have to understand it in depth, but at least we need to know what we should do and what operations are not recommended in every stage of the application life cycle, so that we can develop higher performance, better applications lay a solid foundation.
Is the official WP application execution model diagram.
Pay attention to the following four events:
1. Launching events.
To put it bluntly, it is the event triggered when the application is just started. Due to the special nature of this event, do not perform a lot of operations in the event handler, such as time-consuming jobs, why? Think about it. If a certain operation consumes a lot of time, you will surely find that the program starts very slowly. As a result, the user will be very upset, the user experience is compromised.
2. Activated event.
The application is triggered when it is activated. For example, if there is a button in my program, the user clicks it and opens the "window" for sending a text message. After the user sends the text message, the page for sending the text message is closed, at this time, our application is changed from the background program to the foreground program, and the activated event is triggered. Note that this event is not triggered when the program is started for the first time.
3. deactivated event.
Compared with the activated event, for example, when I click the button in the program, the page that sends the text message will start. At this time, the current application is blocked by the previous text message page. That is to say, when the current application is sent to the background, the deactivated event is triggered at this time. However, if the application is closed, this event is not triggered.
4. Closing events.
You can literally guess when the event happened. Yes, it occurs when the application is closed, but it is not sent to the background in the application's navigation. For example, after opening the text message page, even though the program is sent to the background, however, it is not triggered because it is still running and does not exit. However, if I start the program from the start or desktop tile and then return to the desktop through the return key, this event is triggered because the program exits.
To verify how these events occur, we write the debugging output code for these events in the app. XAML. CS file.
[CSHARP]View plaincopyprint?
- // Code executed when the application is started (for example, starting from the "Start" menu)
- // This code is not executed when the application is re-activated;
- Private void application_launching (Object sender, launchingeventargs E)
- {
- System. Diagnostics. Debug. writeline (datetime. Now. tow.timestring () + "-application started for the first time. ");
- }
- // 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)
- {
- System. Diagnostics. Debug. writeline (datetime. Now. tow.timestring () + "-the application is activated. ");
- }
- // 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)
- {
- System. Diagnostics. Debug. writeline (datetime. Now. tow.timestring () + "-application sleep. ");
- }
- // 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)
- {
- System. Diagnostics. Debug. writeline (datetime. Now. tow.timestring () + "-close the application. ");
- }
Then, we run the program. When the page appears, click "return" on the simulator to close the program.
At this time, let's take a look at the "output" window.
Through this experiment, we found that the activated and deactivated events were not triggered. Why? Think for yourself.
Next, we put a button in the page and click the button to open the text message sending page.
[HTML]View plaincopyprint?
- <! -- Contentpanel-place other content here -->
- <Grid X: Name = "contentpanel" grid. Row = "1" margin = "12,0, 12,0">
- <Button verticalalignment = "TOP"
- Horizontalalignment = "center"
- Content = "Click me"
- Fontsize = "64"
- Click = "button_click"
- Margin = "0, 12, 0, 0"/>
- </GRID>
Write the Click Event code.
[CSHARP]View plaincopyprint?
- Private void button_click (Object sender, routedeventargs E)
- {
- Microsoft. Phone. Tasks. smscomposetask SMS = new Microsoft. Phone. Tasks. smscomposetask ();
- SMS. To = "13672265138 ";
- SMS. Body = "Hello, can I have fried beef at noon today? ";
- SMS. Show ();
- }
Press F5 and then click the button to open the text message page.
Look at the "output" window. At this time, we can see that the deactivated event has occurred, because the program has not exited, but is only put in the background.
Then, click "return" of the simulator and return it to our program. Let's look at the output window.
At this time, the activated event occurs.
OK. Let's blow the cowhide here today.