Windows Phone tombstone (9)

Source: Internet
Author: User


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:

 

 

Life Cycle Description: When we run program a on the start page, program a enters the launched 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.

// 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)
        {
            // can read data from independent storage space for initialization
        }

        // 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)
        {
            // can recover temporary data
           
           
        }

        // 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)
        {
            // Store temporary data so that users can activate from the tombstone state
            
        }

        // 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)
        {
            // When the program presses the Back key, it can be stored in an independent storage space
        }


Effect:

 

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:

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 ();
        }
        // Verified that this method is only loaded when the program starts
        private void PhoneApplicationPage_Loaded (object sender, RoutedEventArgs e)
        {
            //this.ApplicationTitle.Text = ap.appKey;
        }
        protected override void OnManipulationStarted (ManipulationStartedEventArgs e)
        {
            sum ++;
            // show at the title of the program
            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);
        }
       
    }
}


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:

// 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)
         {
             // can recover temporary data
             object str = string.Empty;

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

         // 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)
         {
             // Store temporary data so that users can activate from the tombstone state
             PhoneApplicationService.Current.State ["key"] = appKey;
         } 

We can see that we use it when users encounter emergencies (such as phone 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:


 

 

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;

Source code

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.