Monday, December 24, 2018

Hwo To Working Amongst Information Inwards Your Windows Viii Application

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.
Data is cardinal to most applications, together with agreement how to contend information together with transform it into information the user tin interact alongside is critical. Windows viii applications tin interact alongside information inward a multifariousness of ways. You tin salve local data, think syndicated content from the Web, together with parse local resources that are stored inward JSON format. You tin enquiry XML documents, role WinRT controls to similar a shot the user to pick out files from the file system, together with manipulate collections of information using a structured enquiry language.
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 settings role a elementary lexicon to shop values together with involve the values yous shop to endure basic WinRT types. It is possible to shop to a greater extent than complex types. In Chapter 5, yous learned how to manually serialize together with de-serialize an detail past times writing to a file inward local storage. You serialize complex types using a serialization helper. An representative of this exists inward the SuspensionManager cast that is included inward the projection templates. You tin search for the file SuspensionManager.cs on your arrangement to browse the rootage code.
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.
After the lexicon has been written, the serialization helper is used to de-serialize the information when the application resumes afterwards a termination:
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.