0

In the code-behind of a page of Windows 8.1 app, I create a SerializableDictionary:

private static SerializableDictionary<string, Persona> elenco =
                                  new SerializableDictionary<string, Persona>();

where, the SerializableDictionary class is this. How can I save elenco using Windows.Storage.ApplicationData object and his RoamingSettings property? (If it helps, even changing the declarations private static).

Rohit Vats
  • 79,502
  • 12
  • 161
  • 185
Marco87
  • 13
  • 4

1 Answers1

0

Well, since it's serializable, I guess one way would be to serialize it to a string and store that string in RoamingSettings. Something like this:

var stringBuilder = new StringBuilder();
var xmlWriter = XmlWriter.Create(stringBuilder);
elenco.WriteXml(xmlWriter);
var applicationData = Windows.Storage.ApplicationData.Current;
applicationData.RoamingSettings.Values["elenco"] = stringBuilder.ToString();

I haven't actually used RoamingSettings though, so I could be way off. Here's how you'd read the data back out:

var applicationData = Windows.Storage.ApplicationData.Current;
var elencoXml = (string)applicationData.RoamingSettings.Values["elenco"];
var xmlReader = XmlReader.Create(new StringReader(elencoXml));
elenco.ReadXml(xmlReader);
  • Thank you very much, it seems to work. Now, how can I perform the reverse operation, that is read from `RoamingSettings` and save to a `SerializableDictionary?` – Marco87 Nov 23 '13 at 12:57
  • It looks like the end of your last comment is missing. Are you saying you don't know how to get your data into the SerializableDictionary to begin with? The SerializableDictionary class you posted can be used as a normal dictionary: elenco["some key"] = new Persona(); – Isaac McGarvey Nov 23 '13 at 14:34
  • Thank you very much! I want to post a reply to show the code but I can not for the next 8 hours. I do not have enough reputation. – Marco87 Nov 23 '13 at 14:45