繁体   English   中英

如何创建 Shape 对象的集合?

[英]How to create a collection of Shape objects?

我目前正在 C# Winforms 中开发一个用户控件,它允许您每次从设计器中添加带有属性的图形,以便您更好地理解我,我将参考 DataGridView,每次添加一列,它有几个参数:标题、文本对齐等,都在每个对象列内

好吧,就我而言,我有这门课:

public class Shape
{
    public int X { get; set; }
    public int Y { get; set; }
    public int Width { get; set; }
    public int Height { get; set; }
    public ShapeType ShapeType { get; set; }
    public Color Color { get; set; }
    public bool Mirrored { get; set; }
}

好吧,我的目标是创建一个 Shape 对象的集合,其中在每个 Shape 对象中封装了它们的属性:

private List<Shape> shapelist = new List<Shape>();
public List<Shape> Shapes
{
    get => this.shapelist;
    set => this.shapelist = value;
}

从 Windows 窗体设计器实例化控件时,我在哪里添加参数:

public ShapesDecoration()
{
     InitializeComponent();
     Init();
}

public void Init()
{
     Shapes[0] = new Shape();
     Shapes[0].Width = 148;
     Shapes[0].Height = 64;
     Shapes[0].X = 20;
     Shapes[0].Y = 20;
     Shapes[0].Color = Color.DodgerBlue;
     Shapes[0].ShapeType = ShapeType.Circle;
}

再去调用列表中每个对象的这些参数,比如我这里调用的是第一个位置的Shape对象的参数:

protected override void OnPaint(PaintEventArgs e)
{
     base.OnPaint(e);
     Hector.Framework.Utils.ShapeCollection.FillCircle(e, Shapes[0].Color, Shapes[0].X, Shapes[0].Y, Shapes[0].Width, Shapes[0].Height);
}

我的问题是,每次我将控件拖到窗体上时,Visual Studio 都会向我显示一条错误消息,指出Shape 未标记为可序列化

那么,列表不支持数据类型之类的类吗?

或者我是否错误地执行了某些操作?

如何创建 Shape 对象的集合,每个对象都有各自的参数?

简单的添加属性:

[Serializable]
public class Shape
{
    public int X { get; set; }
    public int Y { get; set; }
    public int Width { get; set; }
    public int Height { get; set; }
    public ShapeType ShapeType { get; set; }
    public Color Color { get; set; }
    public bool Mirrored { get; set; }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM