繁体   English   中英

将控件属性保留到Designer.cs文件

[英]Persisting a control property to the Designer.cs file

我有一个自定义控件,其中包含PointF类型的属性。 将此控件添加到表单并保存后,designer.cs文件不会显示以下内容:

...
this.customControl.LocationF = new System.Drawing.PointF(50.0f, 50.0f);
...

相反,它说:

...
this.customControl.LocationF = ((System.Drawing.PointF)(resources.GetObject("customControl.LocationF")));
...

我一直试图“说服”此属性以正确地序列化到设计器文件,而我的搜索已经找到了一些有希望的潜在客户:

我遵循了MSDN示例中给出的示例,将Point替换为PointF并将int替换为float ,然后我的CustomControl如下所示:

public class CustomControl : Button
{ 
  [Category("Layout")]
  [TypeConverter(typeof(PointFConverter))]
  public PointF LocationF
  {
    get { return this.Location; }
    set { this.Location = new Point((int)value.X, (int)value.Y); }
  }
}

据我所知,这应该可以工作,但似乎对将其序列化到设计器文件的方式没有影响。

别的东西我刚刚注意到-在PointFConverter不产生designer.cs文件时,实际使用过-读取或写入的性能属性的值时,它仅用于在设计模式中...也许这TypeConverter的事死路一条...

简而言之...

如何使控件的属性(在本例中为PointF类型)正确地序列化到窗体的designer.cs文件?

更新资料

现在,我正在查看CodeDomSerializer的子类,它确实更改了designer.cs代码(按照该页面上的示例添加注释,但是似乎只能将其整体应用于CustomControl类,并尝试修改基本序列化,以将CodeCastExpression替换为CodeObjectCreateExpression 不过,这似乎是一种非常凌乱的处理方式。

创建以下类:

public class MyPointF
{
    public float X { get; set; }
    public float Y { get; set; }
}

对您的CustomControl使用以下类定义:

public class CustomButton : Button
{
    private MyPointF _locationF = new MyPointF() { X = 50, Y = 50 };

    [Category("Layout")]
    [DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
    public MyPointF LocationF
    {
        get
        {
            return _locationF;
        }
        set
        {
            _locationF = value;
        }
    }
}

资料来源:

暂无
暂无

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

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