3

In a Windows Store app I can only store WinRT types in the ApplicationSettings, according to the documentation. For roamed settings that should be held together I can use ApplicationDataCompositeValue. Trying to store an instance of an own class or struct results in an Exception with the message " WinRT information: Error trying to serialize the value to be written to the application data store. Additional Information: Data of this type is not supported". The term "trying to serialize" indicates that there must be some way so serialize a type for the application data API.

Does anyone know how I could achieve that?

I tried DataContract serialization but it did not work.

4
  • Did you mark your class as Serializable? Oct 22, 2012 at 20:34
  • @Mike Brown: I tried but SerializableAttribute is not available in .NET API for Windows Store Apps. Oct 23, 2012 at 7:42
  • Ah I wasn't aware of this. Looks like you have a viable alternative though :) Oct 23, 2012 at 13:35
  • Refer to stackoverflow.com/questions/12768796/… for a solution with Custom type
    – LZH
    Oct 17, 2013 at 23:38

1 Answer 1

8

I think custom/own types are not supported.

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

But you can serialize your objects to XML and save as 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 too:

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

Disclaimer: The above links are from my project

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.