次の方法で共有


Loading and saving settings

[This article is for Windows 8.x and Windows Phone 8.x developers writing Windows Runtime apps. If you’re developing for Windows 10, see the latest documentation]

You can store and access app preferences and settings, and even share this data between computers using automatic syncing.

When thinking about saving and loading settings, you need to know a little about the Windows 8.1 and Windows Phone 8.1Application lifecycle first. Specifically, when your app is no longer being used, it's placed in a suspended state. Once suspended, the system decides when to terminate it. You provide the code to save the app's current state and any of your users' settings. When your app resumes, you can then restore it to where the user left off. If you do it right, the user won't even know your app was terminated.

To make saving and loading settings data a simpler process, you can use the ApplicationsData class, part of the Windows.Data namespace. This class provides APIs to share, load, and share app data. There are three types of data storage:

Local LocalSettings lets you store data as a key/value dictionary pair, and LocalFolder lets you store of files. The data is stored on the current computer only, and when the app is deleted, the data is deleted.
Roaming RoamingSettings lets you store data as a key/value dictionary pair, and RoamingFolder lets you store files. The data is stored on the current computer. It's also shared with any other computers that have the app installed and are configured to synchronize settings. When the app is deleted, the data is deleted.
Temporary TemporaryFolder is where an app can store any files it may need while it runs, but the system reserves the right to delete them at any time.

 

Saving and loading app data in C#

In this short C# example, some app settings (a high score) are saved to LocalSettings when the app is suspended, and re-loaded when the app resumes. Assume this code is in App.xaml.cs, and in the methods OnSuspending and OnLaunched, respectively. These methods are created for you when using the Blank App (XAML) project type.

Note  You don't have to reload settings data when an app has resumed after being suspended. The app stays in memory and resumes where it left off. However, if the app has been suspended and then terminated, you do need to restore state. See Application lifecycle (Windows Store apps) for more info about suspending and resuming apps.

 

            // Saving Application Data
            //
            // Add this to  OnSuspending() in App.xaml.cs
            // Add: using Windows.Storage;

            int HighScore = 1000;
            ApplicationData.Current.LocalSettings.Values["HighScore"] = HighScore;


            // Loading Application Data            //
            // Add this to OnLaunched() in App.xaml.cs
            if (args.PreviousExecutionState == ApplicationExecutionState.Terminated)
            {
                // App is being resumed afted being termination, so settings
                // should be restored.

                if (ApplicationData.Current.LocalSettings.Values.ContainsKey("HighScore"))
                {
                    int HighScore;
                    HighScore = (int)(ApplicationData.Current.LocalSettings.Values["HighScore"]);
                }
            }
            else
            {
                // App is being resumed after being suspended, so typically you do not
                // need to restore settings.
            }

Note  You can only save simple data types directly with ApplicationData. If you need to save more complicated objects, you will need to serialize them first. Please see Save a collection to Application Storage and the DataContractSerializer class for information on how to do that.

 

Topics for iOS devs

Resources for iOS devs

Windows 8 controls for iOS devs

Windows 8 cookbook for iOS devs

Lifecycle topics

Application lifecycle (Windows Store apps)

Launching, resuming, and multitasking (Windows Store apps using C#/VB/C++ and XAML)

Launching, resuming, and multitasking (Windows Store apps using JavaScript and HTML)

App data topics

Accessing app data with the Windows Runtime (Windows Store apps)

Guidelines for roaming application data

Guidelines for app settings (Windows Store apps)

Blog: Return of the Fish Based Interface and Serializing Data