1

我一直在尝试为我的统一游戏制作一个 dll,方法是将对象 SaveGame 发送到插件,然后保存到独立存储中。但它不起作用。这是用于保存的代码:

 public static void Save(SaveGame filetobeSaved)
    {

        using (IsolatedStorageFile armz = IsolatedStorageFile.GetUserStoreForApplication())
        {
            if (armz.FileExists("save.gd"))
            {
                armz.DeleteFile("save.gd");
            }
            using (IsolatedStorageFileStream file = new IsolatedStorageFileStream("save.gd", System.IO.FileMode.Create, armz))
            {
                XmlSerializer serializer = new XmlSerializer(typeof(SaveGame));
                serializer.Serialize(file, filetobeSaved);
            }} }

当我使用 IsoStoreSpy 检查此文件时,该文件只有以下内容:

 <?xml version="1.0" encoding="utf-8"?>
 <SaveGame xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" />

为什么不能正确保存?

保存游戏.cs

public class SaveGame
{
    public static int gold { get; set; }
         public static int BalasMais { get; set; }

    public static int DanoMais { get; set; }
         public static int SpeedMais { get; set; }
    public static int LifeMais { get; set; }

    public int getGold() { return gold; }
    public int getBalas() { return BalasMais; }
    public int getDano() { return DanoMais; }
    public int getSpeed() { return SpeedMais; }
    public int getLife() { return LifeMais; }
    public SaveGame()
    {
        gold = 0;
        BalasMais = 0;
        DanoMais = 0;
        SpeedMais = 0;
        LifeMais = 0;
    }
    public SaveGame(int g, int b, int d, int s, int l)
    {
        gold = g;
        BalasMais = b;
        DanoMais = d;
        SpeedMais = s;
        LifeMais = l;
    }

}

}

4

1 回答 1

1

XmlSerializer不序列化静态字段。它只能序列化公共实例的非只读字段和属性。删除static修饰符,您将获得:

<?xml version="1.0" encoding="utf-16"?>
<SaveGame xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <gold>1</gold>
  <BalasMais>2</BalasMais>
  <DanoMais>3</DanoMais>
  <SpeedMais>4</SpeedMais>
  <LifeMais>5</LifeMais>
</SaveGame>

或者,根据您的设计,使用实例属性(而不是方法)包装静态字段。

于 2015-10-23T15:17:57.717 回答