简体   繁体   English

WP7上带有IsolatedStorageSettings的InvalidDataContractException

[英]InvalidDataContractException with IsolatedStorageSettings on WP7

I'm trying to save custom classes in the IsolatedStorage for a WP7 application. 我试图将自定义类保存在WP7应用程序的IsolatedStorage中。

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. 然后在save()方法上抛出InvalidDataContractException。

I know I have to serialize my class places, but i haven't found a good/easy tutorial on google. 我知道我必须序列化我的课堂位置,但是我还没有在Google上找到良好/简单的教程。

Thanks for the help. 谢谢您的帮助。

try this, if this does not work, then refactor your code by storing a simple type for the location, ie a simple class with two properties of type double instead of a GeoCoordinate. 尝试此操作,如果这不起作用,则通过存储该位置的简单类型(即具有两个属性类型为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);
}

I resolved my serialization error myselft using this link : here and here 我使用以下链接自己解决了序列化错误: 此处此处

I created a class called Tools : 我创建了一个名为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 ! 谢谢大家,祝您有美好的一天!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM