【问题标题】:Saving a List of Objects in Windows Phone 7 XNA在 Windows Phone 7 XNA 中保存对象列表
【发布时间】:2011-07-23 19:44:37
【问题描述】:

我需要保存一个对象列表,对象是从我创建的类中创建的。我该怎么办?

我尝试了 XmlSerializer,并将 [XmlElement] 添加到需要序列化的字段中。但它一直给我“XML 文档中有错误”。 我也尝试了 DataContractSerializer,我使用了 [DataContract] 和 [DataMember] 但它不会保存我的对象。

两个存储类都适用于基本元素(int、bool .. 等),但不适用于我的对象。

这是我的保存代码:

        using (IsolatedStorageFile saveGameFile = IsolatedStorageFile.GetUserStoreForApplication())
        using (IsolatedStorageFileStream SaveGameStream = new IsolatedStorageFileStream("GemsCollector1.dat", FileMode.OpenOrCreate, saveGameFile))
        {
            XmlSerializer serializer = new XmlSerializer(typeof(List<Card>));
            serializer.Serialize(SaveGameStream, Cards);
        }

还有这个用于加载:

        using (IsolatedStorageFile saveGameFile = IsolatedStorageFile.GetUserStoreForApplication())
        using (IsolatedStorageFileStream saveGameStream = new IsolatedStorageFileStream("GemsCollector1.dat", FileMode.OpenOrCreate, saveGameFile))
        {
            if (saveGameStream.Length > 0)
            {
                XmlSerializer serializer = new XmlSerializer(typeof(List<Card>));
                Cards = (List<Card>)serializer.Deserialize(saveGameStream);
            }

        }

我的卡类:

public class Card
{
    [XmlElement]
    public CardType CardType { get; set; }

    [XmlElement]
    public CardColor CardColor { get; set; }
    [XmlElement]
    public int Value { get; set; }
    [XmlElement]
    public Vector2 Position { get; set; }
    [XmlElement]
    public PlayerPosition playerPosition { get; set; }
    [XmlElement]
    public CardStatus Status { get; set; }
    public Rectangle BoundingBox
    {
        get
        {
            int width = (playerPosition == PlayerPosition.Left || playerPosition == PlayerPosition.Right) ? 150 : 100;
            int height = (playerPosition == PlayerPosition.Left || playerPosition == PlayerPosition.Right) ? 100 : 150;
            return new Rectangle((int)Position.X, (int)Position.Y, width, height); ;
        }
    }
    [XmlElement]
    public bool isUsed;
    public Vector2 endPosition = new Vector2(235,200);
    public Rectangle ThrowArea = new Rectangle(235, 200, 350, 120);
    [XmlElement]
    public string cardTextureName;
    private string back = "back";
    private static bool ReserveDrag;
    [XmlElement]
    private Vector2 touchFromCenter;
    [XmlElement]
    private int touchId;


    public Card()
    {
    }
}

谁能告诉我我们如何在 XNA 中保存用户定义对象列表?

【问题讨论】:

  • 您是否尝试将其保存到手机的独立存储中?如果是这样,您也许应该发布您的序列化代码。
  • 是的,到IsolatedStroage,我刚刚添加了我的代码
  • 对,所以错误与您尝试序列化的“卡”对象有关。我认为我们可以放心地假设您没有使类属性可序列化。所以你也必须在这里发布 Card 类。
  • 好吧,我刚刚添加了我的课程,我删除了这些方法,因为它们太长了。

标签: xna datacontractserializer xmlserializer windows-phone isolatedstoragefile


【解决方案1】:

您正在尝试序列化私有属性。这在 Windows Phone 7 上不受支持。这很容易导致错误。

此外,您必须确保用于属性的所有类型也是可序列化的,并且所有类型都有一个空的构造函数。

【讨论】:

  • 枚举默认是可序列化的。如果类是,您也不必显式将成员标记为可序列化。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-06-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多