Learn well-nigh the different types of information that are available to your Windows viii application together with techniques for manipulating, loading, storing, encrypting, signing, together with querying data.
In this chapter, yous larn well-nigh the different types of information that are available to your Windows viii application together with techniques for manipulating, loading, storing, encrypting, signing, together with querying data. You’ll notice that the WinRT provides several ready-to-use APIs that brand working alongside information a breeze. This chapter explores these APIs together with how to best integrate them into your application.
Application Settings
You were exposed to application settings inward Chapter 5, Application Lifecycle. Common cases for using application settings include- Simple settings that are accessed through the Settings charm together with tin endure synchronized betwixt machines (Roaming)
- Local information storage persisted betwixt application sessions (Local)
- Local persistent cache to enable occasionally disconnected scenarios (Local)
- Temporary cached information used equally a workspace or to ameliorate performance of the application (Temporary)
The SuspensionManager cast uses the DataContractSerializer to serialize complex types inward a dictionary:
DataContractSerializer serializer = novel 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 cast together with composes XML to stand upward for the class. The XML is written to a file inward the folder allocated for the electrical current application. Similar to the diverse containers for application settings (local, roaming, together with temporary), at that topographic point is a local folder specific to the user together with application that yous tin role to exercise directories together with read together with write files. Accessing the folder is equally elementary as
StorageFile file = expect ApplicationData.Current.LocalFolder. CreateFileAsync(filename, CreationCollisionOption.ReplaceExisting);You tin access a roaming or temporary folder equally well. The Create CompletionOption is a characteristic that allows yous generate filenames that don’t conflict alongside existing data. The options (passed inward equally an enum to the file method) include:
- FailIfExists—The functioning volition throw an exception if a file alongside that advert already exists.
- GenerateUniqueName—The functioning volition append a sequence to the destination of the filename to ensure it is a unique, novel file.
- OpenIfExists—If the file already exists, instead of creating a novel file, the functioning volition exactly opened upward the existing file for writing.
- ReplaceExisting—Any existing file volition endure overwritten. The representative volition ever overwrite the file alongside the XML for the dictionary.
DataContractSerializer serializer = novel DataContractSerializer(typeof(Dictionary<string, object>), knownTypes_); sessionState_ = (Dictionary<string, object>)serializer .ReadObject(inStream.AsStreamForRead());
The local storage tin endure used for to a greater extent than than exactly saving state. As demonstrated inward Chapter 5, yous may likewise role it to shop data. It tin likewise endure used to shop assets similar text files together with images. Influenza A virus subtype H5N1 mutual blueprint is to role local storage to salve cloud-based information that is unlikely to alter equally a local cache. This volition allow your application to operate fifty-fifty when the user is non connected to the Internet together with inward approximately cases may ameliorate the performance of the application when the network is experiencing high latency. In the side past times side section, yous larn to a greater extent than well-nigh how to access together with salve information using the Windows Runtime.