Pending and resuming operations in the Metro app

Source: Internet
Author: User

Windows 8 applications usually involve two data types: Application Data and session data. This article mainly explains the Session Layer. The session layer data is formed based on the user's use of the application each time. The data may not need to be stored on the device. During the entire application lifecycle, the application enters the running state after it is started. When the user leaves or the system enters the STANDBY state, the application will be suspended, and the application will be put into the memory. When the user re-uses the application, it will return to the running state.

Next, we will focus on how to save the data when it is suspended.

Add a basic page to a new project. You cannot add a blank page because several files are automatically added to the project when you add a basic page. These files are stored in the common Folder:

Here we mainly use layoutawarepage. CS (for navigation and State management of process lifecycle management) and suspensionmanager. CS (capture global session status to simplify process lifecycle management of applications) files.

  1. The application will be suspended.

1) Open the app. XAML. CS program and create a rootframe in the onlaunched method. When the rootframe is null, the frame will be re-created. In this logical judgment, useSuspensionmanager. registerframeIn this way, the application can obtain the root frame information and store the data.

Protected async override void onlaunched (launchactivatedeventargs ARGs)
{
Frame rootframe = Window. Current. content as frame;

// Do not repeat application initialization when the window contains content,
// Make sure that the window is active
If (rootframe = NULL)
{
// Create a framework to act as the navigation context and navigate to the first page
Rootframe = new frame ();
Suspensionmanager. registerframe (rootframe, "appframe ");
If (ARGs. previusexecutionstate = applicationexecutionstate. Terminated)
{
// Todo: Load status from the previously suspended application
}

// Put the frame in the current window
Window. Current. content = rootframe;
}

If (rootframe. content = NULL)
{
// When the navigation stack is not restored, navigate to the first page,
// Configure it by passing in the required information as a navigation Parameter
If (! Rootframe. navigate (typeof (mainpage )))
{
Throw new exception ("failed to create initial page ");
}
}
// Make sure that the current window is active

Window. Current. content = rootframe;
Window. Current. Activate ();
}

Note: The red code registers a pending event for the current frame. Here, we only register an event for the frame of a page. We can register multiple pages.

2) In the onsuspending method, useSuspensionmanager. saveasyncThe method saves the current state of the suspended application. asynchronous operations can be called here for processing.

/// <Summary>
/// Called when the application is to be suspended for execution. When you do not know the application
/// Save the application status when it is terminated or restored,
/// And keep the memory content unchanged.
/// </Summary>
/// <Param name = "sender"> source of the pending request. </Param>
/// <Param name = "E"> details about pending requests. </Param>
Private async void onsuspending (Object sender, suspendingeventargs E)
{
// The app. XAML. CS file contains the processing program of the application. Suspending event. Windows will call this event handler when it is about to suspend an application.
// This is the time to save the application status to Prevent the application from being terminated. We use the suspensionmanager class to simplify the session state saving process.
// This class saves the application's navigation status and provides you with the opportunity to save the session Status of the activity page.

VaR deferral = E. suspendingoperation. getdeferral ();
// Todo: Save the application status and stop any background activity.
Await suspensionmanager. saveasync ();// Call saveasync to save the navigation status of the frame,
// Then provide the page with the opportunity to save its content.

Deferral. Complete ();
}

Note: When the user suspends an application, the program will enter this method, and then run the red code above. Next, the program will go to the system to process the pending operation, and then the savestate method on the page will be called, process the data to be saved.

3) Open mainpage. XAML. CS and addSavestateMethod, add the following code to save the current username field when the user is suspended.

Protected override void savestate (Dictionary <string, Object> pagestate)
{
Pagestate ["show"] = this. Show. text;
}

 At this point, the above is to save the data at the Session Layer when the application is suspended. Here, it only saves simple string-type data. If you need to save more complex types, further modification is required. For more information, see the next article.

2. restore operation

1) After the pending operation is complete, restore the temporary data to the application. Open app. XAML. CS again in the onlaunched MethodPreviusexecutionstate is determined as terminatedJoinSuspensionmanager. restoreasyncMethod to restore the previous application status.

Protected async override void onlaunched (launchactivatedeventargs ARGs)
{
Frame rootframe = Window. Current. content as frame;

If (rootframe = NULL)
{
Rootframe = new frame ();
Suspensionmanager. registerframe (rootframe, "appframe ");
If (ARGs. previusexecutionstate = applicationexecutionstate. Terminated)
{
// Todo: Load status from the previously suspended application

       Suspensionmanager. restoreasync ();
}

 

// Put the frame in the current window
Window. Current. content = rootframe;
}

 

If (rootframe. content = NULL)
{
If (! Rootframe. navigate (typeof (mainpage )))
{
Throw new exception ("failed to create initial page ");
}
}
// Make sure that the current window is active

 

Window. Current. content = rootframe;
Window. Current. Activate ();
}

 

2) Add in mainpage. XAML. CSLoadstateThe Code is as follows:

/// <Summary>
/// Fill the page with the content passed during the navigation process. In a previous session
/// When you recreate a page, any saved status is also provided.
/// </Summary>
/// <Param name = "navigationparameter"> when you first request this page
/// <See CREF = "frame. navigate (type, object)"/> parameter value.
/// </Param>
/// <Param name = "pagestate"> Status of this page retained during previous sessions
/// Dictionary. The first time the page is accessed, It is null. </Param>
Protected override void loadstate (Object navigationparameter, Dictionary <string, Object> pagestate)
{
If (pagestate! = NULL & pagestate. containskey ("show "))
{
This. Show. Text = pagestate ["show"]. tostring ();
}
}

  Note that if the mainpage. XAML. CS page contains the onnavigatedto method and you overwrite this method, you need to reference loadstate yourself. Otherwise, an error may occur. If the onnavigatedto method is not overwritten, the system will call the onnavigatedto method of the mainpage parent class. The loadstate method is referenced in this method. You can also add the following code page.

Protected override void onnavigatedto (navigationeventargs E)
{
Base. onnavigatedto (E );
}

In addition, in the app. XAML. CS file:

Public app ()
{
This. initializecomponent ();
This. Suspending + = onsuspending;
This. Resuming + = onresuming;

}

The red code is the event when the restoration is added. When the event changes from pending to restoring, it first enters the onresuming event, where the stored data can be processed, and then in the input page.

 

 

 

 

 

 

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.