0

我有一个 IList 类,但是当我尝试将它们存储在 IsolatedStorage 中时,它只是说内置的序列化程序无法处理它,JSON.net 也不能​​。我已经把这个类放在下面了,有人能想出一种存储它的方法吗?

我得到的错误是;

无法序列化类型“System.Windows.UIElement”。考虑使用 DataContractAttribute 属性对其进行标记,并使用 DataMemberAttribute 属性标记您想要序列化的所有成员。

IList<ScoreWatcher> RecentSessions = new List<ScoreWatcher>();
public class ScoreWatcher
{
    public ScoreWatcher() { }

    public string SessionName = "";
    public DateTime SessionCreationTime;
    public DateTime SessionModificationTime;

    public int Player1Total = 0;
    public int Player1ScoreRollover = 0;
    public int Player2Total = 0;
    public int Player2ScoreRollover = 0;

    public string Player1Name = "";
    public string Player2Name = "";

    public ListBox scoreListBox;

    public string GrabFriendlyGLobal()
    {
        UpdateModificationTime();
        return string.Format("{0}-{1}", Player1Total, Player2Total);
    }

    public void UpdateModificationTime()
    {
        SessionModificationTime = DateTime.Now;
    }

    public void UpdateScoringSystem()
    {
        UpdateModificationTime();

        Player1Total = 0;
        Player1ScoreRollover = 0;
        Player2Total = 0;
        Player2ScoreRollover = 0;


        foreach (Match snookerMatch in matches)
        {
            if (snookerMatch.Player1Score > snookerMatch.Player2Score)
                Player1Total++;
            else if (snookerMatch.Player1Score == snookerMatch.Player2Score)
            {
                Player1Total++;
                Player2Total++;
            }
            else
                Player2Total++;

            // House cleaning
            Player1ScoreRollover += snookerMatch.Player1Score;
            Player2ScoreRollover += snookerMatch.Player2Score;
        }

    }
    public void LoadMatchesIntoListbox()
    {
        UpdateModificationTime();

        scoreListBox.Items.Clear();

        foreach (Match snookerMatch in matches)
            scoreListBox.Items.Add(new UserControls.GameHistoryTile(snookerMatch.GlobalScore, snookerMatch.Player1Score, snookerMatch.Player2Score));
    }

    public List<Match> matches = new List<Match>();
    public class Match
    {
        public int Player1Score = 0;
        public int Player2Score = 0;

        public string GlobalScore = "0-0";
    }
}
4

1 回答 1

1

您只能序列化具体类

理想情况下,您需要更改您的实现以提供序列化程序的具体类型 - 您需要将其作为 IList 吗?

编辑:啊,我看到你没有序列化一个接口 - 基本上你在你的类上有一个 UIElement 的引用。您需要指定它被 XmlSerializer 忽略 - 这些事件处理程序是否由表单处理?

编辑 2:仅供参考,如果您想这样做,您可以使用 XmlIgnore 属性或 NonSerialized,具体取决于您是使用 BinaryFormatter 还是 XmlSerializer 进行序列化

例如

[XmlIgnore]
public int SomeProperty { get; set; }
于 2012-07-06T18:42:51.263 回答