Working with Data in Your Windows 8 Application
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 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 learn about the different types of data that are available to your Windows 8 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 explores these APIs and how to best integrate them into your application.
Application Settings
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, 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. An example of this exists in the SuspensionManager class that is included in the project templates. You can search for the file SuspensionManager.cs on your system to browse the source code.
The SuspensionManager class uses the DataContractSerializer to serialize complex types in a dictionary:
DataContractSerializer serializer = new DataContractSerializer(typeof(Dictionary<string, object>), knownTypes_); serializer.WriteObject(sessionData, sessionState_);
The serializer (in this case, the DataContractSerializer class) 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 Create CompletionOption is a feature that allows you generate filenames that don’t conflict with existing data. The options (passed in as an enum to the file method) include:
- FailIfExists—The operation will throw an exception if a file with 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.
After 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 Chapter 5, you may also use it to store data. It can also be used to store assets like text files and images. 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 learn more about how to access and save data using the Windows Runtime.