保存Windows手机中的对象列表7 XNA
题
我需要保存一个对象列表,这些对象是由我制作的类创建的。我应该怎么办?
我尝试了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中的用户定义对象列表吗?
解决方案
您正在尝试序列化私人属性。这在Windows Phone 7上不支持。这很容易导致错误。
另外,您必须确保您用于属性的所有类型都可以序列化,并且所有类型都有一个空的构造函数。
不隶属于 StackOverflow