Quickly build Windows 8 style Apps 27-roaming app data

Source: Internet
Author: User

Original: Quickly build Windows 8 style Apps 27-roaming app data

This blog post focuses on roaming app data overview, how to build roaming app data, and build roaming app data best practices.

I. Overview of roaming Application Data

1. If roaming application data is used in the application, it is easy for users to keep the application data synchronized between different devices.

2.Windows synchronizes the updated roaming data to the cloud and updates the data to other devices where the app is installed.

3.Windows limits the size of app data that each app can roam. If our app reaches the limit of roaming data size, no data from the app will be updated to the cloud until the app's total roaming data is less than that limit again, so it's best to use roaming data only for user preferences, links, and small data files.

4. As long as the user accesses the app's roaming data from a device within the required time interval, the data will be in the cloud, and if the user does not run the app for longer than this time interval, the roaming data will be removed from the cloud, and if the user uninstalls the app, the roaming data will not automatically be deleted from the cloud and will be retained If the user reinstalled the app during that time interval, roaming data will be synced from the cloud. Note: The time interval is specified as 30 days.

5.Windows randomly roams app data and does not guarantee instant synchronization. Roaming can be significantly delayed if the user is offline or in a high-latency network. Then for important time-critical settings, more frequent updates can be made with a particularly high-priority setup unit. It is limited to a specific set of units named "Highpriority". It can be a complex, but the total size is limited to 8KB. This limit is not mandatory, and when this limit is exceeded, setting units or setting the complex is considered a general set unit or complex.

second, how to build roaming application Data

1. Register the event that the roaming data changed, the event is the datachanged event.

void Inithandlers ()
{
   
      New Object> (Datachangehandler);
}
void Object o)
{
   Update data
}

2. Get the settings and file container for your app. Usually we use the applicationdata.roamingsettings property to get the settings and Applicationdata.roamingfolder properties to get the file.

Windows.Storage.ApplicationDataContainer roamingsettings = Windows.Storage.ApplicationData.Current.RoamingSettings;
Windows.Storage.ApplicationDataContainer roamingfolder = Windows.Storage.ApplicationData.Current.RoamingFolder;

3. Write the data to the settings.

Use the applicationdatacontainer.values property for simple data writing.

roamingsettings.values["examplesetting" "Hello World";

Use the applicationdatacompositevalue object for composite settings.

New Windows.Storage.ApplicationDataCompositeValue ();
composite["Intval"] = 1;
composite["Strval" "string";
roamingsettings.values["examplecompositesetting"] = composite;

Use the Applicationdatacontainer.createcontainer method to create a settings container.

   Roamingsettings.createcontainer ("Examplecontainer", Windows.Storage.ApplicationDataCreateDisposition.Always);
if (RoamingSettings.Containers.ContainsKey ("Examplecontainer"))
{
   roamingsettings.containers["Examplecontainer"]. values["examplesetting" "Hello World";
}

4. Get the data from the settings.

Get the simple data.

value = roamingsettings.values["examplesetting"];

Gets the data in the composite settings.

   (Windows.Storage.ApplicationDataCompositeValue) roamingsettings.values["examplecompositesetting"];
if null)
{
   No data
}
Else
{
   Access data in composite["Intval"] and composite["Strval"]
}

Gets the data in the container.

bool Hascontainer = RoamingSettings.Containers.ContainsKey ("Examplecontainer");
BOOL false;
if (Hascontainer)
{
   hassetting = roamingsettings.containers["Examplecontainer"]. Values.containskey ("examplesetting");
}

5. Delete the data from the settings.

Use the applicationdatacontainersettings.remove method to delete the data in the settings.

RoamingSettings.Values.Remove ("examplesetting");

6. Write the data to the file.

Using Windows.Storage.StorageFolder.CreateFileAsync and Windows.Storage.FileIO.WriteTextAsync You can create and update files in the roaming app data store.

void Writetimestamp ()
{
   
       New Windows.Globalization.DatetimeFormatting.DateTimeFormatter ("longtime");
   StorageFile Samplefile = await Roamingfolder.createfileasync ("DataFile.txt"
       createcollisionoption.replaceexisting);
   Await Fileio.writetextasync (Samplefile, formatter. Format (DateTime.Now));
}

7. Get the data from the file.

Use Windows.Storage.StorageFolder.GetFileAsync, Windows.Storage.StorageFile.GetFileFromApplicationUriAsync and Windows.Storage.FileIO.ReadTextAsync You can open and read files in the roaming app data store.

void Readtimestamp ()
{
   Try
   {
      StorageFile Samplefile = await Roamingfolder.getfileasync ("DataFile.txt");
      String timestamp = await fileio.readtextasync (samplefile);
      Data is contained in timestamp
   }
   Catch (Exception)
   {
      Timestamp not found
   }
}

iii. Best Practices for building roaming application Data

Matters to be done:

1. Use roaming for preferences and customizations. Users will always choose to have all app roaming data set on each device, such as user preferences, which can include: favorite teams, background color customization, and so on.

2. Use roaming data to allow users to continue to perform tasks across devices. For example: The last reading position when reading an article, the level or score information in the game, and so on.

Prohibited matters:

1. Do not use roaming for information that is part of your local device.

For example, the path name of a local file resource in a PC. This information should not be included in roaming app data and must be retained as local device information. However, you can still decide to roam the local information, but only if this information is not valid on another device, the app should be able to recover this information perfectly.

2. Do not use roaming to move large datasets

There is a limit to the size of app data that each app can roam. If the app reaches this limit, the data cannot roam until the app's total roaming app data is again smaller than the limit. It is therefore a best practice to restrict roaming to user preferences, links, and small data files. During application design, it is important to consider how to set a limit for large data to avoid exceeding this limit.

For example, if you need 10KB for each game state you save, your app may only allow users to store up to 10 games.

3. Do not use roaming for instant syncing, or for changing frequent information

Windows roams app data randomly and does not guarantee instant synchronization.

If the user is offline or in a high-latency network, roaming can be significantly delayed, so do not build a UI that requires immediate synchronization.

If your app frequently changes information (for example, the latest location in a song or movie), do not use roaming app data for this data, and choose a representation that is less frequent but can still provide a good user experience.

For example, the currently playing song, the currently playing movie chapter, and so on.

For important time-critical settings, more frequent updates can be made with a particularly high-priority setup unit.

For more information on roaming application data, refer to:

1. QuickStart: Roaming application data;

2. Roaming Application Data Guide;

3. Manage application data;

Example code for roaming app data is provided in MSDN: Application Data Sample.

Quickly build Windows 8 style Apps 27-roaming app data

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.