在WP7上使用IsolatedStorageSettings的InvalidDataContractException

时间:2013-08-06 14:09:38

标签: c# wpf windows-phone-7

我正在尝试在IsolatedStorage中为WP7应用程序保存自定义类。

这是班级:

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;
    }
}

以下是我正在尝试做的一个例子:

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();

我在save()方法上抛出一个InvalidDataContractException。

我知道我必须序列化我的班级名额,但我还没有在google上找到一个好的/简单的教程。

感谢您的帮助。

2 个答案:

答案 0 :(得分:1)

尝试这个,如果这不起作用,那么通过存储一个简单的位置类型来重构代码,即一个简单的类,它有两个属性类型为double而不是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);
}

答案 1 :(得分:0)

我使用此链接解决了序列化错误myselft: herehere

我创建了一个名为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);
            }
        }
    }

我创建了两个函数:

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;
        }

感谢所有人,祝你有个美好的一天!