Before WP development, as with other development technologies, we need to understand the life cycle of a WP application simply, we do not have to understand, but at least we need to know at each stage of the application life cycle, what we should do, what is not recommended, this is to let us develop higher performance, Better applications to lay a solid foundation.
Is the official given WP application execution Model diagram.
In, we should note the following four events:
1, launching event.
Plainly speaking, is the application just started when the event triggered, due to this event a little particularity, try not to do a lot of operations in the event handler, such as the time-consuming job, why? You know, if one of your operations is going to take a lot of time, then you'll definitely find that the program starts very slowly, which makes the user uncomfortable and the user experience compromised.
2, activated event.
The application is activated when the trigger, for example, my program has a button, the user click after the development of the text message "window", when the user sent a text message, the page is closed, this time, our application from the background program into the foreground program, activated event will trigger, note that The event is not triggered the first time the program is started.
3, deactivated event.
Relative to the activated event, such as the above example, when I click on the program button, the text message will start the page, this time, the current application is blocked by the front text message page, that is, the current program is sent to the background, this moment triggered the deactivated event. However, if the application is closed, the event is not triggered.
4, closing event.
It literally guessed when the event happened. Yes, when the application is closed, but it does not occur when the application is sent to the background in the navigation, such as just said the development of the text message page, although the program was sent to the background, but because it is still running, no exit, so this time does not trigger. But if I start the program from "Start" or the desktop tile and then go back to the desktop with the "Back" key, this event triggers because the program exits.
To verify how these events occur, in the App.xaml.cs file, write down the debug output code for each of these events.
- // Code executed when the application starts (for example, from the Start menu)
// This code is not executed when the application is reactivated;
private void Application_Launching (object sender, LaunchingEventArgs e)
{
System.Diagnostics.Debug.WriteLine (DateTime.Now.ToShortTimeString () + "-The application starts for the first time.");
}
// Code executed when the application is activated (in the foreground)
// This code is not executed when the application is first launched
private void Application_Activated (object sender, ActivatedEventArgs e)
{
System.Diagnostics.Debug.WriteLine (DateTime.Now.ToShortTimeString () + "-application is activated.");
}
// Code executed when the application is deactivated (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.ToShortTimeString () + "-application sleeps.");
}
// The code executed when the application closes (for example, 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.ToShortTimeString () + "-application closed.");
}
Then we run the program, and when the main page appears, click the "Back" button on the emulator to close the program.
At this point, let's take a look at the Output window.
Through this experiment, we found that activated events and deactivated events were not triggered, why? Think for yourself.
Below, we put a button in the page, click on the button, open the page to send text messages.
- <!--Contentpanel-place other content here--
- <grid x:name= "Contentpanel" grid.row= "1" margin= "12,0,12,0" >
- <button verticalalignment= "Top"
- Horizontalalignment= "Center"
- Content= "Please click on Me"
- Fontsize= "64"
- click= "Button_Click"
- margin= "0,12,0,0"/>
- </Grid>
Write the Click event code.
- private void Button_Click (object sender, RoutedEventArgs e)
- {
- Microsoft.Phone.Tasks.SmsComposeTask SMS = new Microsoft.Phone.Tasks.SmsComposeTask ();
- Sms. to = "13672265138";
- Sms. Body = "Hello, would you like to have beef fried rice with me this noon?" ";
- Sms. Show ();
- }
Press F5 to run, then click on the button to develop SMS page.
Look at the "Output" window, at this time, see the deactivated event occurred, because the program has not exited, but was put in the background.
Next, click on "Back" of the simulator and return to our program. Then look at the Output window.
This time, the activated incident happened.
OK, today's cowhide is blown here.
Windows Phone Development (3): The pawn is not moved, the first view of the global