簡體   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