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.
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 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.
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.
1 comments:
edirne
hatay
ığdır
ısparta
muş
5W7
Post a Comment