Windows Phone notes (9) use independent storage (on)

Source: Internet
Author: User
ArticleDirectory
    • Use the isolatedstoragesettings class to save application settings

In the previous notes, we learned how to share data between pages, but the data is kept in the memory, whenProgramThe stored data will be lost when it is terminated. In many cases, it is necessary to persist the data, such as the configuration and startup information of the storage program. In Windows Phone, we can achieve data persistence through independent storage. However, to improve system security,All I/O operations in Windows Phone applications are limited to independent storage, and only independent storage under the application directory can be accessed..

First, according to the data type to be stored, the Use Types of Windows Phone independent storage are as follows:The following three types, We will introduce the following independent storage types and how to use them.

  • Set: UseIsolatedstoragesettingsClass stores data as key/value pairs.

  • Files and folders: UseIsolatedstoragefileClass storage files and folders.

  • Link data: Use LINQ to SQL to store relational data in a local database.

Use the isolatedstoragesettings class to save application settings

In our applications, we often make some settings for the application to meet the needs of the application or adapt to their own operation habits. For example, changing the topic, skin, and startup settings of an application. To implement these functions, we must make persistent changes and settings for our team's applications. Even if the program is closed, the stored data can be stored in the same scope next time, in Windows Phone, we useIsolatedstoragesettings classSet the application,This class uses key-value pairs to store data.. The following is a simple example to demonstrate how to use the isolatedstoragesetting class to store data.

The role of this example isSaves the background color before exiting the page, overwrites the running program, and loads the restored background color.. There is only one mainpage.CodeYou do not need to modify it. This is here.

First, we still need to use the app class we used in the previous note, because the instance of this class exists in the entire application lifecycle, all of them are suitable for sharing data in the entire application. Here weAdd an attribute to the app class to save the background color and add two methods to save and load the settings of the program respectively.Make sure that our app class is a branch class (You can split the definition of classes, structures, and interfaces into two or more source files ):

 1       Public   Partial   Class APP: Application
2 {
3 // Application settings
4 Public Brush mainpagebackgroundbrush { Get ; Set ;}
5
6 /// <Summary>
7 /// Load settings
8 /// </Summary>
9 Void Loadsettings ()
10 {
11 Isolatedstoragesettings settings = isolatedstoragesettings. applicationsettings;
12 Color mainpageclr;
13 // Check whether the background color is saved.
14 If (Settings. trygetvalue <color> (" Mainpagebackgroundcolor " , Out Mainpageclr ))
15 {
16 This . Mainpagebackgroundbrush = New Solidcolorbrush (mainpageclr ); // Load the saved settings
17 }
18 }
19
20 /// <Summary>
21 /// Save settings
22 /// </Summary>
23 Void Savesettings ()
24 {
25 Isolatedstoragesettings settings = isolatedstoragesettings. applicationsettings;
26
27 If ( This . Mainpagebackgroundbrush Is Solidcolorbrush)
28 {
29 Settings [ " Mainpagebackgroundcolor " ] = ( This . Mainpagebackgroundbrush As Solidcolorbrush). color; // Save settings
30 }
31 Settings. Save ();
32
33 }
34 }

Now there is a problem. When should we save the settings? When should I load the settings? We can write relevant code by taking advantage of the lifecycle characteristics of the application page in Windows Phone. In Windows Phone, we can use four main events of application state management (included in the Application Object) to complete our operations. They are:

  • Launching:Occurs when the application starts.

  • Deactivated:This occurs when the application is deactivated.

  • Activated:This occurs when the application changes to active after it is in sleep or logically deleted state.

  • Closing:Occurs when the application exits.

Windows Phone application retentionStatus.

Ten-second principle: If it takes more than 10 seconds to save the temporary state, the system terminates the application.

After learning about the status management events of these four programs, we can easily answer the previous questions:When the launching event and activated event occur, we load the settings;When deactivated events and closing events occur, we keep the settings. Next we will put the call to save and load the setting method in the event status management functions that have been added to the template generated by the Windows Phone project. below isApp. XAML. CSAdded code:

 1  // Code executed when the application is started (for example, starting from the Start menu)
2 // This code is not executed when the application is re-activated.
3 Private Void Application_launching ( Object Sender, launchingeventargs E)
4 {
5 Loadsettings (); // Load settings
6 }
7
8 // Code executed when the application is activated (placed on the frontend)
9 // This code is not executed when the application is started for the first time.
10 Private Void Application_activated ( Object Sender, activatedeventargs E)
11 {
12 Loadsettings (); // Load settings
13 }
14
15 // Code executed when the application is disabled (sent to the background)
16 // This code is not executed when the application is closed
17 Private Void Application_deactivated (Object Sender, deactivatedeventargs E)
18 {
19 Savesettings (); // Save settings
20 }
21
22 // Code executed when the application is closed (for example, when the user clicks back)
23 // This code is not executed when the application is disabled
24 Private Void Application_closing ( Object Sender, closingeventargs E)
25 {
26 Savesettings (); // Save settings
27 }

 

The preparations have been completed, so weMainpage. XAML. CSBackground processing code:

 1   Public   Partial  Class Mainpage: phoneapplicationpage
2 {
3 Random ran = New Random ();
4 // Constructor
5 Public Mainpage ()
6 {
7 Initializecomponent ();
8 // Set the access app class for independent storage
9 Brush = (application. Current As APP). mainpagebackgroundbrush;
10
11 If (Brush! = Null )
12 {
13 This . Contentpanel. Background = brush;
14 }
15 }
16
17 Protected Override Void Onmanipulationstarted (manipulationstartedeventargs E)
18 {
19 Solidcolorbrush brush = New Solidcolorbrush (color. fromargb
20 ( 255 ,( Byte ) Ran. Next ( 255 ),( Byte ) Ran. Next ( 255 ),( Byte ) Ran. Next ( 255 )));
21 This . Contentpanel. Background = brush;
22
23 // To use the independent storage settings, save the settings to the app class
24 (Application. Current As APP). mainpagebackgroundbrush = brush;
25
26 Base . Onmanipulationstarted (E );
27 }
28 }

Compile and run the program:

Run the program to randomly generate a background color, and then click "back" and "start" on the mobile phone to return to our application, we can see that the background color has been loaded as the settings maintained when the last program exited.

Okay, so here we know how to useIsolatedstoragesettingsClass to keep the application settings. We will learn howUseIsolatedstoragefileClass storage files and folders.

 

References:

Http://msdn.microsoft.com/zh-cn/library/ff817008 (V = vs.92). aspx (important)

Http://msdn.microsoft.com/zh-cn/library/ff967547 (V = vs.92). aspx

Http://msdn.microsoft.com/zh-cn/library/hh307995.aspx)

Http://msdn.microsoft.com/zh-cn/library/ff967548 (V = vs.92). aspx

Programming Windows Phone 7 Microsoft Silverlight Edition

Author: Sunny pig

Source: Http://www.cnblogs.com/IPrograming

The copyright of this article is shared by the author and the blog Park. You are welcome to repost this article. However, you must retain this statement without the author's consent and provide a clear link to the original article on the article page. Otherwise, you will be held legally liable.

Windows Phone developer exchange group: 79339880. You are welcome to discuss and exchange and learn and make progress together.

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.