2012-07-06 136 views
0

我有一个IList类,但是当我尝试将它们存储在IsolatedStorage中时,它只是说内置的seralizer无法处理它,JSON.net也不能。我已经把课程放在下面,任何人都可以想出一种方法来存储它?在独立存储中存储IList

我得到的错误是;

类型'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"; 
    } 
} 
+0

你需要一个具体的类 - 你不能序列化的界面,据我所知 – Charleh 2012-07-06 18:40:54

+0

在具体的类研究后,它看起来像什么,我贴的? – Purify 2012-07-06 18:43:36

+0

没有接口是实现的契约 - 它定义的行为不是状态 - 具体的类是提供实现的类。任何接口都不是可序列化的(尽管我假设你正在序列化整个列表......不仅仅是一个项目,因为ScoreWatcher是具体的--IList 不是) – Charleh 2012-07-06 18:47:57

回答

1

您可以只序列具体的类

理想情况下,你需要改变你的实施提供了串行具体类型 - 你需要这是一个IList?

编辑:啊我看你没有序列化一个接口,然后 - 基本上你有一个UIElement的参考你的类。你需要指定这个被XmlSerializer忽略 - 这些事件处理程序是由表单处理的吗?

编辑2:仅供参考,如果你想这样做,你可以使用XmlIgnore属性或非序列化取决于如果您使用的BinaryFormatter或XmlSerializer的做系列化

例如

[XmlIgnore] 
public int SomeProperty { get; set; } 
+0

我可以使用列表。这会更好吗? – Purify 2012-07-06 18:48:44

+0

是的,这将工作 – Charleh 2012-07-06 18:48:51

+0

只是改变它,以便它使用列表。我仍然得到同样的错误。我更新了错误的第一篇文章。 – Purify 2012-07-06 18:51:27