gpt4 book ai didi

c# - System.Runtime.Serialization.InvalidDataContractException Windows Phone 8

转载 作者:太空宇宙 更新时间:2023-11-03 16:16:27 25 4
gpt4 key购买 nike

我正在尝试将一个对象保存到独立存储中,但收到 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);
}
}

}

对象包含对象和控件的列表。

enter image description here

要保存的主对象里面的对象列表 enter image description here

是不是因为我不能序列化像图像控件和 Canvas 控件这样的对象?将此类对象保存到隔离存储中的最佳方法是什么?

更新 1

我已将我的对象简化为仅包含以下属性,但仍然出现相同的错误。 Points 和 Char 是可序列化的吗?

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

最佳答案

设法解决了它。我通过删除可能无法使用 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);

关于c# - System.Runtime.Serialization.InvalidDataContractException Windows Phone 8,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15680967/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com