Reputation: 1360
I'm trying to save custom classes in the IsolatedStorage for a WP7 application.
Here is the class :
public class places
{
public GeoCoordinate coordonnees { get; set; }
public string nom { get; set; }
public places(GeoCoordinate coo, string _nom)
{
this.coordonnees = coo;
this.nom = _nom;
}
}
Here is an example of what I'm trying to do :
List<places> liste;
if (IsolatedStorageSettings.ApplicationSettings.Contains("places"))
{
liste = (List<places>)IsolatedStorageSettings.ApplicationSettings["places"];
} else {
liste = new List<places>();
}
liste.Add(new places(this.position_actuelle, this.name.Text));
IsolatedStorageSettings.ApplicationSettings["places"] = liste;
IsolatedStorageSettings.ApplicationSettings.Save();
And I throw an InvalidDataContractException on the save() method.
I know I have to serialize my class places, but i haven't found a good/easy tutorial on google.
Thanks for the help.
Upvotes: 1
Views: 519
Reputation: 1360
I resolved my serialization error myselft using this link : here and here
I created a class called Tools :
public static class Tools
{
public static string Serialize(object obj)
{
using (MemoryStream memoryStream = new MemoryStream())
using (StreamReader reader = new StreamReader(memoryStream))
{
DataContractSerializer serializer = new DataContractSerializer(obj.GetType());
serializer.WriteObject(memoryStream, obj);
memoryStream.Position = 0;
return reader.ReadToEnd();
}
}
public static object Deserialize(string xml, Type toType)
{
using (Stream stream = new MemoryStream())
{
byte[] data = System.Text.Encoding.UTF8.GetBytes(xml);
stream.Write(data, 0, data.Length);
stream.Position = 0;
DataContractSerializer deserializer = new DataContractSerializer(toType);
return deserializer.ReadObject(stream);
}
}
}
And i created two functions :
private List<places> deserialize_places(List<string> l)
{
List<places> liste = new List<places>();
foreach(string s in l){
liste.Add((places)Tools.Deserialize(s, typeof(places)));
}
return liste;
}
private List<string> serialize_places(List<places> l)
{
List<string> liste = new List<string>();
foreach (places s in l)
{
liste.Add(Tools.Serialize(s));
}
return liste;
}
Thx all and have a good day !
Upvotes: 0
Reputation: 2111
try this, if this does not work, then refactor your code by storing a simple type for the location, i.e a simple class with two properties of type double instead of a GeoCoordinate.
public static class SettingsStorageManager
{
/// <summary>
/// Save an object to isolated storage.
/// </summary>
/// <param name="key">
/// The key to store the object with.
/// </param>
/// <param name="value">
/// object to store.
/// </param>
public static void Save<T>(string key, T value)
{
if (IsolatedStorageSettings.ApplicationSettings.Contains(key))
{
IsolatedStorageSettings.ApplicationSettings[key] = value;
}
else
{
IsolatedStorageSettings.ApplicationSettings.Add(key, value);
}
IsolatedStorageSettings.ApplicationSettings.Save();
}
/// <summary>
/// Gets an object from the isolated storage based on a key. when object not found, returns a default value of T.
/// </summary>
/// <param name="key">
/// The key used to store the object.
/// </param>
public static T TryGet<T>(string key)
{
if (!IsolatedStorageSettings.ApplicationSettings.Contains(key))
return default(T);
return (T) IsolatedStorageSettings.ApplicationSettings[key];
}
}
public static class SettingsStorageFactory
{
/// <summary>
/// Get's a list of locations from storage.
/// </summary>
public static IEnumerable<places> StoragePlaces
{
get
{
return SettingsStorageManager.TryGet<IEnumerable<places>>("places").ToSafeList();
}
}
}
public static class IsolatedStorageExtensions
{
public static IEnumerable<T> ToSafeList<T>(this IEnumerable<T> list)
{
if (list == null) return Enumerable.Empty<T>();
return list;
}
}
public static class IsolatedStorageExtensions
{
public static IEnumerable<T> ToSafeList<T>(this IEnumerable<T> list)
{
if (list == null) return Enumerable.Empty<T>();
return list;
}
}
public class MyCallingClass
{
var places = SettingsStorageFactory.StoragePlaces;
places.Add(new places(this.position_actuelle, this.name.Text)).ToList();
SettingsStorageManager.Save("places", places);
}
Upvotes: 1