我正在尝试将对象保存到独立存储中但收到A first chance exception of type 'System.Runtime.Serialization.InvalidDataContractException' occurred in System.Runtime.Serialization.ni.dll
我的对象类
[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);
}
}
}
该对象包含一个对象和控件列表。
要保存的主对象内的对象列表
是不是因为我无法序列化图像控件和画布控件等对象?将此类对象保存到隔离存储中的最佳方法是什么?
更新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 :(得分: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);
答案 1 :(得分:0)
我使用这个类并在保存时抛出相同的异常
System.Runtime.Serialization.ni.dll中出现“System.Runtime.Serialization.InvalidDataContractException”类型的异常,但未在用户代码中处理
[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;
}
}