Windows Phone development (3): The pawns are not moved.

Source: Internet
Author: User

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

// 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 () + "-application starts for the first time.");
        }

        // Code executed when the application is activated (put in the foreground)
        // 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.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 sleep.");
        }

        // Code executed when the application is closed (for example, the user clicks "Back")
        // This code is not executed when the application is deactivated
        private void Application_Closing (object sender, ClosingEventArgs e)
        {
            System.Diagnostics.Debug.WriteLine (DateTime.Now.ToShortTimeString () + "-application is closed.");
        }
Then, we run the program. When the main page appears, click the "Back" button on the simulator to close the program.

At this time, let's take another 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 on the page, and click the button to open the page for sending SMS.

        <!-ContentPanel-Put other content here->
        <Grid x: Name = "ContentPanel" Grid.Row = "1" Margin = "12,0,12,0">
            <Button VerticalAlignment = "Top"
                    HorizontalAlignment = "Center"
                    Content = "Please click me"
                    FontSize = "64"
                    Click = "Button_Click"
                    Margin = "0,12,0,0" />
        </ Grid>
Write 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, will you invite me to eat fried rice with beef at noon today?";
            sms.Show ();
        }
Press F5 to run, and then click the button to open the texting page.

Looking at the "Output" window, at this time, you can see that the Deactivated event has occurred, because the program has not yet exited, but has been placed in the background.

Then, click "Back" in the simulator to return to our program. Look at the output window again.

At this time, the Activated event occurred.

OK, today's cowhide is here.

Related Article

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.