2013-08-06 57 views
1

我試圖保存WP7應用程序的IsolatedStorage中的自定義類。InvalidDataContractException與WP7上的IsolatedStorageSettings

這裏是類:

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上找到好的/簡單的教程。

感謝您的幫助。

+0

查找有關如何序列化和反序列化對象的教程。然後,只需將這些新知識應用於封裝實際IsolatedStorageSettings的新類中,該類將爲您完成此序列化。 – Aphelion

+0

我試過這個tuto:http://blogs.msdn.com/b/darrylru/archive/2011/05/04/datacontract-serialization-with-generics-amp-read-only-properties.aspx 不起作用,生成的xml很糟糕。 –

+0

什麼蹩腳的xml?如果真的很重要,你可以使用這些屬性來格式化你的xml,或者在繼承自'List '的類上實現'IXmlSerializable'接口。 – Aphelion

回答

0

我使用這個鏈接解決了我的序列化錯誤myselft: herehere

我創建了一個類調用工具:

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

Thx all and have好日子 !

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); 
} 
+0

Thx爲此,但我仍然有一個InvalidDataContractException在您的「保存」方法。 我將嘗試將我的類解析爲XML,這可以解決我的問題 –