Десериализация в список и рисование их снова - PullRequest
0 голосов
/ 16 октября 2018

Я сериализовал класс (Shape Class) в свое собственное расширение файла (файл .seal).

 public void Serialization()
    {
            //Open the File Dialog for Saving
            SaveFileDialog save = new SaveFileDialog();
            //Custome File Extension
            save.Filter = "Seal Creator File (*.seal)|*.seal";
            save.DefaultExt = ".seal";
            save.AddExtension = true;

            if (save.ShowDialog() == DialogResult.OK)
            {
                //Serializers
                XmlSerializer ser = new XmlSerializer(typeof(List<Shape>));
                //XmlSerializer ser2 = new XmlSerializer(typeof(DataTable));
                FileStream file = File.Create(save.FileName);
                //Serialization process for the Class and DataSet
                ser.Serialize(file, draw._shapes);
                //ser2.Serialize(file, dataTable);
                file.Close();
                MessageBox.Show("Saved");
            }
    }

Примерно так:

<?xml version="1.0"?>
<ArrayOfShape xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <Shape>
    <size>
      <Width>480</Width>
      <Height>415.6922</Height>
    </size>
    <location>
      <X>10</X>
      <Y>12</Y>
    </location>
    <radius>0</radius>
    <type>triangle</type>
    <width>480</width>
    <height>415.6922</height>
    <strokeThickness>2</strokeThickness>
    <color />
    <userDefinedWidth>0</userDefinedWidth>
    <userDefinedHeight>0</userDefinedHeight>
    <userDefinedStroke>0</userDefinedStroke>
  </Shape>
</ArrayOfShape>

Теперь, что я хочу сделать,заключается в том, чтобы снова сохранить значения в списке и перерисовать их в графическом окне.

    public void DrawAllShapes(object sender, PaintEventArgs e)
    {
        foreach (Shape shape in _shapes)
        {
            switch (shape.type)
            {
                case Shape.ShapeType.rectangle:
                    shape.DrawRectangle(shape.color, shape.strokeThickness, shape.width , shape.height, e.Graphics, shape.radius);
                    break;
                case Shape.ShapeType.square:
                    shape.DrawSquare(shape.color, shape.strokeThickness, shape.width, shape.height, e.Graphics, shape.radius);
                    break;
                case Shape.ShapeType.circle:
                    shape.DrawCircle(shape.color, shape.strokeThickness, shape.width , shape.height , e.Graphics);
                    break;
                case Shape.ShapeType.ellipse:
                    shape.DrawEllipse(shape.color, shape.strokeThickness, shape.width, shape.height, e.Graphics);
                    break;
                case Shape.ShapeType.triangle:
                    shape.DrawTriangle(shape.color, shape.strokeThickness, shape.width, e.Graphics, shape.radius);
                    break;
            }
        }

Десериализация:

public void Deserialize(StreamReader file)
    {
        try
        {
            List<Shape> shape_ = new List<Shape>();
            XmlSerializer ser = new XmlSerializer(shape_.GetType());
            List<Shape> shape = (List<Shape>)ser.Deserialize(file);
            shape_ = shape;
            shape_.ForEach(draw._shapes.Add);
            file.Close();
            pictureBox_Canvass.Invalidate();
            Debug.WriteLine(shape_.Count.ToString());
        }
        catch (Exception ex)
        {
            //Catching the Error
            String innerMessage = (ex.InnerException != null)? ex.InnerException.Message : "";
            Debug.WriteLine(innerMessage);
            MessageBox.Show("error");
        }
    }

Редактировать: Добавлен метод десериализации.Теперь кажется, что я могу получить список фигур, но я все еще могу перерисовать их в картинке.

...