数据序列化 WPF

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20036418/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 10:04:07  来源:igfitidea点击:

Data Serialization WPF

c#wpfserializationdeserialization

提问by user2602079

Sorry if this question seems silly. I have studied a few web pages on the subject but am unsure how relevant they are to my issue. I have a 2D map designer that i have made. I would like to save the map, so that i can load it and play a game on it. I'm very new to Serialization, and i was wondering what documents i should study and if someone could point me to some web pages that relate to my serialization task. Here is the data structure:

对不起,如果这个问题看起来很愚蠢。我已经研究了一些有关该主题的网页,但不确定它们与我的问题的相关性。我有一个我制作的 2D 地图设计器。我想保存地图,以便我可以加载它并在上面玩游戏。我对序列化非常陌生,我想知道我应该研究哪些文档,以及是否有人可以将我指向一些与我的序列化任务相关的网页。下面是数据结构:

    public class ModelMap
    {
        public ModelMap()
        {
            this.cellBgImage = new Image();
            this.exitImage = new Image();
            this.theseus = new Image();
            this.minotaur = new Image();
        }

        public int rows { get; set; }
        public int cols { get; set; }
        public int boardXPos { get; set; }
        public int boardYPos { get; set; }
        public int myCellSize { get; set; }
        public Image cellBgImage { get; set; }
        public string HorizontalWallSource { get; set; }
        public string VerticalWallSource { get; set; }
        public Image minotaur { get; set; }
        public Image theseus { get; set; }
        public Image exitImage { get; set; }
        public string exitCellPlacement { get; set; }
        public int myWidth { get; set; }
        public int myHeight { get; set; }
        private List<Cell> m_cells = new List<Cell>();
        public List<Cell> myCells
        {
            get
            {
                return m_cells;
            }
            set
            {
                m_cells = value;
            }
        }
    }
}

Cell Class:

细胞类别:

public class Cell
    {
        public Cell(int col, int row, CellSide rightWall, CellSide bottomWall, ModelMap map)
        {
            this.myColumn = col;
            this.myRow = row;
            this.myRightWall = rightWall;
            this.myBottomWall = bottomWall;
            this.myMap = map;
        }

        public Cell(int col, int row, CellSide rightWall, CellSide bottomWall, ModelMap map, Image bgImage)
        {
            this.myColumn = col;
            this.myRow = row;
            this.myRightWall = rightWall;
            this.myBottomWall = bottomWall;
            this.myMap = map;
            this.myBgImage = bgImage;
        }

        public ModelMap myMap { get; set; }
        public Image myBgImage { get; set; }
        public bool hasMinotaur { get; set; }
        public bool hasTheseus { get; set; }
        public bool isExit { get; set; }
        public int mySize { get; set; }
        public CellSide myRightWall { get; set; }
        public CellSide myBottomWall { get; set; }
        public int myColumn { get; set; }
        public int myRow { get; set; }
    }

    public class CellSide
    {
        public CellSide(int hasWall, bool highlighted)
        {
            this.hasWall = hasWall;
            this.isHighlighted = highlighted;
        }

        public bool isHighlighted { get; set; }
        public int hasWall { get; set; }
    }
}

Thank you.

谢谢你。

回答by Konrad Kokosa

Read about standard .NET serializationand examples of XML serialization. You will need to make some changes to make serialization of those classes possible:

阅读标准.NET 序列化XML 序列示例。您需要进行一些更改以使这些类的序列化成为可能:

  • Celland CellSidemust have parameterless constructors,
  • Image(or rather its implementation) can't be directly serialized, but you can make some workaround (like in this question) or just serialize file path to the images and load them after deserialization
  • Cell并且CellSide必须有无参数的构造函数,
  • Image(或者更确切地说它的实现)不能直接序列化,但是你可以做一些解决方法(就像在这个问题中一样)或者只是序列化图像的文件路径并在反序列化后加载它们

Then you can serialize an object to the XML file:

然后你可以将一个对象序列化到 XML 文件:

var map = new ModelMap();
...
System.Xml.Serialization.XmlSerializer serializer = new System.Xml.Serialization.XmlSerializer(typeof(ModelMap));
using (var writer = new StreamWriter(@"e:\test.xml"))
{
    serializer.Serialize(writer, map);
}