Windows Phone app life cycle and data retention

Source: Internet
Author: User

Windwos phone app life cycle you can refer to the MSDN introduction, which is described in detail. Http://msdn.microsoft.com/zhcn/library/windows/apps/xaml/hh464925.aspx

Next, we'll focus on saving the data when the application hangs.

We can save the data by following these three ways.

1, through the Suspensionmanager class

2, through the Navigationhelper class

3, through the api,applicationdata.current.localsettings provided by Microsoft

Note: Suspensionmanager and Navigationhelper are the two classes that Microsoft has packaged for us, right-click Solution, add New Item--basic page, and after adding the basic page vs will automatically generate a common folder.

1. Use Suspensionmanager to maintain the state of the application when it hangs

①, first find App.xaml.cs file

②, registering in the Onlaunched event Suspensionmanager

// notify to  save data                 when registering Suspensionmanager program hangs " Appframe ");

③, saving pending data in event onsuspending

  await  Suspensionmanager.saveasync ();

④, loading state in onlaunched event

if (E.previousexecutionstate = = applicationexecutionstate.terminated)                {                    //  TODO: Loading state                    from a previously suspended application await Suspensionmanager.restoreasync ();                                    }

2, the use of Navigationhelper class to rely on Suspensionmanager, before using the Navigationhelper class to configure the Suspensionmanager, method as above

①, application hangs save data, in Navigationhelper_savestate

Private void Navigationhelper_savestate (object  sender, Savestateeventargs e)        {            e.pagestate.add ("  txt", txtName.Text);        }

②, loading and saving data in the Navigationhelper_loadstate event

Private void Navigationhelper_loadstate (object  sender, Loadstateeventargs e)        {                        if (E. pagestate!=null&&e.pagestate.containskey ("txt"))            {                 = e.pagestate["txt"asstring;            }                   }

3. Using Microsoft to provide API

 //  save data  Windows.storage.applicationdata.current.localsettings.values[ txt  ] = txtName.Text; 
           // Get Data            string str = windows.storage.applicationdata.current.localsettings.values["txt"  asstring;             if NULL )            {                = str;            }


Windows Phone app life cycle and data retention

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.