Windows Phone Development (3): The piece is not moved, the first view of the global

Source: Internet
Author: User



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.

  1. // 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.


    1. <!--Contentpanel-place other content here--
    2. <grid x:name= "Contentpanel" grid.row= "1" margin= "12,0,12,0" >
    3. <button verticalalignment= "Top"
    4. Horizontalalignment= "Center"
    5. Content= "Please click on Me"
    6. Fontsize= "64"
    7. click= "Button_Click"
    8. margin= "0,12,0,0"/>
    9. </Grid>





Write the Click event code.


    1. private void Button_Click (object sender, RoutedEventArgs e)
    2. {
    3. Microsoft.Phone.Tasks.SmsComposeTask SMS = new Microsoft.Phone.Tasks.SmsComposeTask ();
    4. Sms. to = "13672265138";
    5. Sms. Body = "Hello, would you like to have beef fried rice with me this noon?" ";
    6. Sms. Show ();
    7. }





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


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.