IT tutorials
 
Mobile
 

Windows Phone 8 : Exploring the Execution Model (part 3) - Programmatically Exiting an App , Saving Transient State

3/25/2014 1:56:24 AM
- Free product key for windows 10
- Free Product Key for Microsoft office 365
- Malwarebytes Premium 3.7.1 Serial Keys (LifeTime) 2019

3. Programmatically Exiting an App

Prior to Windows Phone 8, the only way to programmatically exit a XAML-based app was to raise and not handle an exception. It was an inelegant hack for a not-uncommon scenario.

In Windows Phone 8, however, the API includes a direct mechanism for terminating an app. It is achieved using the Application class’s Terminate method, as demonstrated:

Application.Current.Terminate();


Note

Use the Terminate method only when the user is unable to make any useful forward progress. For example, if the user is presented with an End User License Agreement and declines it, the app may choose to terminate. Or if the app requires the user to log in and the user cannot provide valid credentials, the app may display a message and then terminate. You should not use this method to provide a general exit mechanism.



Caution

The Terminate method does not raise any application life cycle events. Before you call this method, you must save the state of the app.

4. Saving Transient State

The Deactivated event provides an application with the opportunity to save its transient and persistent state.

The goal is to enable restoration of the application to its prior state before being tombstoned. It should be assumed, however, that when the Deactivated event occurs, that the application is going to be closed, moving to the closed state. The user may, after all, opt not to resume the application, or may use the Start Experience to re-launch the application, rather than using the hardware Back button to return to the application. Moreover, if the user launches many other apps, your app may get bumped off the end of the Back button application stack.

The Visual Studio new project templates place an empty handler for the Deactivated event in the App class. See the following excerpt:

void Application_Deactivated(object sender, DeactivatedEventArgs e)
{
    /* Save transient state like so:
     * PhoneApplicationService.Current.State["DataContractKey"]
     *        = DataContract;
     */

    /* Save persistent state like so:
     * IsolatedStorageSettings.ApplicationSettings["Key"] = someObject; */
}

You can also subscribe to the Deactivated event elsewhere, in the following manner:

PhoneApplicationService.Current.Deactivated += OnDeactivated;

void OnDeactivated(object o, DeactivatedEventArgs args)
{
//...
}


Caution

The operating system gives an app 10 seconds when the PhoneApplicationService.Closing event occurs, before it is forcibly terminated. If the time required to save your app’s state exceeds this amount, its state should be saved periodically, and perhaps incrementally, while it is running.



Note

The Windows Phone emulator terminates an application if it takes longer than 10 seconds to display its first visual. Therefore, when debugging an Activated or Launched event, if this time is exceeded the application exits and the debugger detaches before any UI elements can be shown.


Transient State Requirements

All objects to be stored in the PhoneApplicationService.State property must meet one of the following requirements:

- It is a primitive type.

- It is a known serializable reference type including decimal, string, or DateTime, with a matching System.Convert.ToString method signature.

- It is capable of being serialized using a DataContractSerializer. To achieve this, it must be decorated with a System.Runtime.Serialization.DataContract attribute. Each property or field intended for serialization must be decorated with the DataMember attribute, and in turn each serializable property or field type must be decorated with the DataContract attribute.

Storing an application’s transient state can be difficult because objects containing state often have event subscriptions to or from other objects that are not serialized. Also, types from third-party libraries are usually not decorated with the DataContract attribute, preventing serialization.

To avoid difficulties in serializing objects, keep classes that you intend to serialize as simple as possible, without dependencies on third-party classes. A design pattern that is commonly used for this situation is the Memento Pattern.


 
Others
 
- Windows Phone 8 : Exploring the Execution Model (part 2) - Life Cycle Events
- Windows Phone 8 : Exploring the Execution Model (part 1) - Application State
- Windows Phone 7 to Windows Phone 8 : App publication (part 7) - Selective targeting - Device memory
- Windows Phone 7 to Windows Phone 8 : App publication (part 6) - Selective targeting - Device capabilities
- Windows Phone 7 to Windows Phone 8 : App publication (part 5) - Beta testing, Versions
- Windows Phone 7 to Windows Phone 8 : App publication (part 4) - Updates
- Windows Phone 7 to Windows Phone 8 : App publication (part 3) - Dev Center reports
- Windows Phone 7 to Windows Phone 8 : App publication (part 2) - The publication process
- Windows Phone 7 to Windows Phone 8 : App publication (part 1) - Preparing for publication
- Windows Phone 8 : Share Menu Extensibility (part 2) - A Simple Photo Upload Share Application
 
 
Top 10
 
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Finding containers and lists in Visio (part 2) - Wireframes,Legends
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Finding containers and lists in Visio (part 1) - Swimlanes
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Formatting and sizing lists
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Adding shapes to lists
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Sizing containers
- Microsoft Access 2010 : Control Properties and Why to Use Them (part 3) - The Other Properties of a Control
- Microsoft Access 2010 : Control Properties and Why to Use Them (part 2) - The Data Properties of a Control
- Microsoft Access 2010 : Control Properties and Why to Use Them (part 1) - The Format Properties of a Control
- Microsoft Access 2010 : Form Properties and Why Should You Use Them - Working with the Properties Window
- Microsoft Visio 2013 : Using the Organization Chart Wizard with new data
Technology FAQ
- Is possible to just to use a wireless router to extend wireless access to wireless access points?
- Ruby - Insert Struct to MySql
- how to find my Symantec pcAnywhere serial number
- About direct X / Open GL issue
- How to determine eclipse version?
- What SAN cert Exchange 2010 for UM, OA?
- How do I populate a SQL Express table from Excel file?
- code for express check out with Paypal.
- Problem with Templated User Control
- ShellExecute SW_HIDE
programming4us programming4us