Is there a way to save instances of my own classes in the ApplicationSettings of a Windows Store app?

In the Windows Store application, I can store WinRT types in ApplicationSettings, according to the documentation. For moved settings that should be held together, I can use ApplicationDataCompositeValue. An attempt to save an instance of its own class or structure throws an exception with the message "WinRT information: an error occurred while trying to serialize the value that should be written to the application data store. Additional information: data of this type is not supported." The term "serialization attempt" indicates that the application data type API must be serialized in some way.

Does anyone know how I could achieve this?

I tried serializing a DataContract, but that didn't work.

+3
windows-8 windows-store-apps windows-runtime configuration
source share
1 answer

I think custom / native types are not supported.

See http://msdn.microsoft.com/en-us/library/windows/apps/hh464917.aspx : "Windows Runtime data types are supported for application settings."

But you can serialize your objects in XML and save as a string ... (see code below)

public static string Serialize(object obj) { using (var sw = new StringWriter()) { var serializer = new XmlSerializer(obj.GetType()); serializer.Serialize(sw, obj); return sw.ToString(); } } public static T Deserialize<T>(string xml) { using (var sw = new StringReader(xml)) { var serializer = new XmlSerializer(typeof(T)); return (T)serializer.Deserialize(sw); } } 

https://github.com/MyToolkit/MyToolkit/blob/master/src/MyToolkit/Serialization/XmlSerialization.cs

Check out this class:

https://github.com/MyToolkit/MyToolkit/wiki/XmlSerialization

Disclaimer: The above links apply to my project

+8
source share