Question

I am porting a Windows Phone 8 app to Windows 8.1 and would like to use ApplicationData.Current.LocalSettings to store/persist some data. Beside some String/Bool/Int values I would like to store a (quite simple) custom class as well.

[DataContract]
public class MyClass {
    [DataMember]
    public double Property1;

    [DataMember]
    public int Property2;

    [DataMember]
    public int Property3;

    [DataMember]
    public bool Property4;


    public int Total{
        get { return Property2 + Property 3; }     
        }
    }
}

On WP 8 I use IsolatedStorageSettings.ApplicationSettings to store the settings without any problem. It works fine even if I do not use DataContract/DataMember:

MyClass mySettings = new MyClass();
mySettings.Property2 = 1;
mySettings.Property2 = 2;
IsolatedStorageSettings.ApplicationSettings["mySettings"] = mySettings;
IsolatedStorageSettings.ApplicationSettings.Save();

MyClass loadedSettings = IsolatedStorageSettings.ApplicationSettings["mySettings"];
Debug.WriteLine(loadedSettings.Total); // == 3

The same thing on Win 8.1 results in a serialization exception when using:

...
ApplicationData.Current.LocalSettings.Values["mySettings"] = mySettings;


Error trying to serialize the value to be written to the application data store
  at System.Runtime.InteropServices.WindowsRuntime.IMap`2.Insert(K key, V value)
  at System.Runtime.InteropServices.WindowsRuntime.MapToDictionaryAdapter.Insert[K,V](IMap`2 _this, K key, V value)
  at System.Runtime.InteropServices.WindowsRuntime.MapToDictionaryAdapter.Indexer_Set[K,V](K key, V value)

I found the hint to use DataContract/DataMember but this did not change anything? Any idea how to solve this?

Was it helpful?

Solution

From documentation (http://msdn.microsoft.com/en-us/library/windows/apps/hh464917.aspx):

You can't directly assign other types of objects to app data. You can serialize data to one of the supported data types, for example you can serialize your data as JSON and store it as a string, but you need to handle the serialization.

So you need to serialize objects by yourself or use ApplicationDataCompositeValue for grouping.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top