加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

c# – 数据序列化WPF

发布时间:2020-12-16 01:58:27 所属栏目:百科 来源:网络整理
导读:对不起,这个问题似乎很愚蠢.我已经研究了一些关于这个主题的网页,但我不确定它们对我的问题有多重要.我有一个2D地图设计师.我想保存地图,以便我可以加载它并在其上玩游戏.我对序列化很新,我想知道我应该研究哪些文档,以及是否有人可以指向一些与我的序列化任
对不起,这个问题似乎很愚蠢.我已经研究了一些关于这个主题的网页,但我不确定它们对我的问题有多重要.我有一个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;
            }
        }
    }
}

细胞类:

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,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; }
    }
}

谢谢.

解决方法

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

> Cell和CellSide必须具有无参数构造函数,
>图像(或者说它的实现)不能直接序列化,但你可以做一些解决方法(如in this question)或只是序列化图像的文件路径并在反序列化后加载它们

然后,您可以将对象序列化为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);
}

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读