2

我正在尝试将对象保存到隔离存储中但收到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

我已将对象简化为仅包含以下属性,但仍然出现相同的错误。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; }
4

2 回答 2

0

设法解决了它。我通过删除可能未使用将对象复制到另一个对象但删除某些属性 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);
于 2013-03-28T14:57:26.077 回答
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;
    }
} 
于 2013-12-19T08:16:32.363 回答