2013-03-28 219 views
2

我想一個對象保存到隔離存放,但收到A first chance exception of type 'System.Runtime.Serialization.InvalidDataContractException' occurred in System.Runtime.Serialization.ni.dllSystem.Runtime.Serialization.InvalidDataContractException的Windows Phone 8

我的對象類

[DataContract] 
public class NoteContent : IEnumerable 
{ 
    [DataMember] 
    public Microsoft.Phone.Controls.SupportedPageOrientation SupportedOrientation{ get; set; } 
    [DataMember] 
    public List<Character> NoteCharacterList { get; set; } 
    [DataMember] 
    public string NoteFileName { get; set; } 

    private List<Character> notecontent = new List<Character>(); 

    public void Add(Character charObj) 
    { 
     notecontent.Add(charObj); 
     NoteCharacterList = notecontent; 
    } 

    public void Clear() 
    { 
     notecontent.Clear(); 
    } 

    public Character this[int id] 
    { 
     get { return notecontent[id]; } 
    } 

    public IEnumerator<Character> GetEnumerator() 
    { 
     return notecontent.GetEnumerator(); 
    } 

    IEnumerator IEnumerable.GetEnumerator() 
    { 
     return GetEnumerator(); 
    } 
} 

我使用序列化和保存的代碼目的。錯誤在這行_mySerializer.WriteObject(targetFile, sourceData);

public void SaveMyData(NoteContent sourceData, String targetFileName) 
    { 
     string TargetFileName = String.Format("{0}/{1}.dat", TargetFolderName, targetFileName); 

     using (IsolatedStorageFile store = IsolatedStorageFile.GetUserStoreForApplication()) 
     { 

      if (!store.DirectoryExists(TargetFolderName)) 
       store.CreateDirectory(TargetFolderName); 
      try 
      { 
       using (var targetFile = store.CreateFile(TargetFileName)) 
       { 
        _mySerializer.WriteObject(targetFile, sourceData); 
       } 
      } 
      catch (Exception e) 
      { 
       store.DeleteFile(TargetFileName); 
      } 
     } 

    } 

對象包含對象和控件列表提高。

enter image description here

主要對象內對象的列表保存 enter image description here

是不是因爲我不能像序列圖像控制和帆布控制對象?將此類對象存入獨立存儲的最佳方法是什麼?

更新1

我已經簡化我的對象只包括以下屬性,但我仍然得到同樣的錯誤。點和字符串是否可序列化?

public Point ImagePosition { get; set; } 
    public Char CharText { get; set; } 
    public double ImageDegree { get; set; } 
    public double ImageScale { get; set; } 
    public int ImageZOrder { get; set; } 
    public bool IsResizeCancel { get; set; } 
    public double MaxSliderValue { get; set; } 
    public double CurrentWidth { get; set; } 
    public double CurrentHeight { get; set; } 
+0

爲什麼你不能這樣做?因爲你打算序列化數據,而不是整個UI樹。 –

+0

所以,如果我刪除畫布和圖像控件和複合變換對象,它應該沒問題? – PutraKg

+0

也許 - 這取決於您試圖保存的所有對象是否可串行化。 –

回答

0

管理解決它。我通過消除使用Copying object to another object but remove certain properties Windows Phone 8

簡化類

[DataContract] 
public class CharacterClone 
{ 
    [DataMember] 
    public Point ImagePosition { get; set; } 
    [DataMember] 
    public Char CharText { get; set; } 
    [DataMember] 
    public double ImageDegree { get; set; } 
    [DataMember] 
    public double ImageScale { get; set; } 
    [DataMember] 
    public int ImageZOrder { get; set; } 
    [DataMember] 
    public bool IsResizeCancel { get; set; } 
    [DataMember] 
    public double MaxSliderValue { get; set; } 
    [DataMember] 
    public double CurrentWidth { get; set; } 
    [DataMember] 
    public double CurrentHeight { get; set; } 

    public CharacterClone(Character original) 
    { 
     ImagePosition = original.ImagePosition; 
     CharText = original.CharText; 
     ImageDegree = original.ImageDegree; 
     ImageScale = original.ImageScale; 
     ImageZOrder = original.ImageZOrder; 
     IsResizeCancel = original.IsResizeCancel; 
     MaxSliderValue = original.MaxSliderValue; 
     CurrentWidth = original.CurrentWidth; 
     CurrentHeight = original.CurrentHeight; 
    } 
} 

,並注意到另一個重要的事情是文件路徑可能不序列化特性簡化了我的對象。在WP8的分離應該是\\代替/

string TargetFileName = String.Format("{0}\\{1}.dat", TargetFolderName, targetFileName); 
0

我使用這個類,並在保存它拋出同樣的異常

型「System.Runtime.Serialization.InvalidDataContractException」的異常出現在System.Runtime.Serialization.ni.dll但未在用戶代碼中處理

[DataContract] 
class UserInfo :SerializationException 
{ 
    // Constructors: 
    public UserInfo() { } 
    public UserInfo(String username,String password) { 
     Username = username; 
     Password = password; 
    } 
    [DataMember] 
    public String Username 
    { 
     get; 
     set; 
    } 
    [DataMember] 
    public String Password 
    { 
     get; 
     set; 
    } 
}