This excerpt is from the Rough Cuts version of the book and may not represent the final version of this material.
Data is central to most applications and understanding how to manage data and transform it into information the user can interact with is critical. Windows 8 Metro applications can interact with data in a variety of ways. You can save local data, retrieve syndicated content from the web and parse local resources that are stored in JSON format. You can query XML documents, use WinRT controls to direct the user to select files from the file system and manipulate collections of data using a structured query language.
In this chapter you’ll learn about the different types of data that are available to your Metro application and techniques for manipulating, loading, storing, encrypting, signing, and querying data. You’ll find that the WinRT provides several ready-to-use APIs that make working with data a breeze. This chapter will explore these APIs and how to best integrate them into your application.
You were exposed to application settings in Chapter 5, “Application Lifecycle.” Common cases for using application settings include:
- Simple settings that are accessed through the Settings charm and can be synchronized between machines (Roaming)
- Local data storage persisted between application sessions (Local)
- Local persistent cache to enable occasionally disconnected scenarios (Local)
- Temporary cached data used as a workspace or to improve performance of the application (Temporary)
The settings use a simple dictionary to store values, and require the values you store to be basic WinRT types. It is possible to store more complex types. In Chapter 5, “Application Lifecycle,” you learned how to manually serialize and de-serialize an item by writing to a file in local storage. You serialize complex types using a serialization helper. You can download a library of Windows 8 Metro samples from Microsoft online at:
Many of the examples include a sample class called the SuspensionManager. This class uses the DataContractSerializer to serialize complex types in a dictionary:
DataContractSerializer serializer = new DataContractSerializer(typeof(Dictionary<string, object>), knownTypes_); serializer.WriteObject(outStream.AsStreamForWrite(), sessionState_);
The serialization helper automatically inspects the properties on the target class and composes XML to represent the class. The XML is written to a file in the folder allocated for the current application. Similar to the various containers for application settings (local, roaming, and temporary) there is a local folder specific to the user and application that you can use to create directories and read and write files. Accessing the folder is as simple as:
StorageFile file = await ApplicationData.Current.LocalFolder.CreateFileAsync(filename, CreationCollisionOption.ReplaceExisting);
You can access a roaming or temporary folder as well. The CreateCollisionOption is a feature that allows you generate filenames that don’t conflict with existing data. The options include:
- FailIfExists – the operation will throw an exception if a file of that name already exists
- GenerateUniqueName – the operation will append a sequence to the end of the filename to ensure it is a unique, new file
- OpenIfExists – if the file already exists, instead of creating a new file the operation will simply open the existing file for writing
- ReplaceExisting – any existing file will be overwritten
The example will always overwrite the file with the XML for the dictionary. Once the dictionary has been written, the serialization helper is used to de-serialize the data when the application resumes after a termination:
DataContractSerializer serializer = new DataContractSerializer(typeof(Dictionary<string, object>), knownTypes_); sessionState_ = (Dictionary<string, object>)serializer .ReadObject(inStream.AsStreamForRead());
The local storage can be used for more than just saving state. As demonstrated in the previous chapter (Chapter 5, “Application Lifecycle”) you may also use it to store data. A common design is to use local storage to save cloud-based data that is unlikely to change as a local cache. This will allow your application to operate even when the user is not connected to the Internet, and in some cases may improve the performance of the application when the network is experiencing high latency. In the next section you will learn more about how to access and save data using the Windows Runtime.