在 Windows Phone 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 中保存用户定义对象列表?
i need to save a List of Objects, The Objects are created from a class i made. what should i do?
i tried the XmlSerializer, and i added the [XmlElement] to the fields i need to be serialized. but it kept giving me "There is an Error in XML Document".
I tried also the DataContractSerializer, and i used the [DataContract] and [DataMember] but it wont save my objects.
both storage classes work for basic elements (int,bool .. etc) but not my objects.
heres my code for saving:
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);
}
and this one for loading:
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);
}
}
my Card Class:
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()
{
}
}
can anybody please tell me how we save List of userdefined objects in XNA?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您正在尝试序列化私有属性。 Windows Phone 7 不支持此功能。这很容易导致错误。
此外,您还必须确保用于属性的所有类型也是可序列化的,并且所有类型都有一个空的构造函数。
You're attempting to serialize private properties. That's not supported on Windows Phone 7. Which could easily be cause for the error.
Also, you have to ensure that all of the types you used for properties, is also serializeable, and that all types have a empty constructor.