简体   繁体   English

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

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

I have a custom control which contains a property of type PointF . 我有一个自定义控件,其中包含PointF类型的属性。 When this control is added to a form and is saved, the designer.cs file doesn't say something like: 将此控件添加到表单并保存后,designer.cs文件不会显示以下内容:

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

Instead, it says this: 相反,它说:

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

I've been trying to "persuade" this property to properly serialise to the designer file, and my search has turned up a couple of promising leads: 我一直试图“说服”此属性以正确地序列化到设计器文件,而我的搜索已经找到了一些有希望的潜在客户:

I've followed the example given in the MSDN example, replacing Point with PointF and int with float , then my CustomControl looks like this: 我遵循了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); }
  }
}

As far as I can see, this should work, but it seems to have no effect on how it's serialised to the designer file. 据我所知,这应该可以工作,但似乎对将其序列化到设计器文件的方式没有影响。

Something else I've just noticed - the PointFConverter isn't actually ever used when generating the designer.cs file - it's only used when reading or writing the value of the property in the properties box in design mode... Maybe this TypeConverter thing is a dead end... 别的东西我刚刚注意到-在PointFConverter不产生designer.cs文件时,实际使用过-读取或写入的性能属性的值时,它仅用于在设计模式中...也许这TypeConverter的事死路一条...

In Short... 简而言之...

How do I make a control's property (specifically in this case a PointF type) serialise correctly to a form's designer.cs file? 如何使控件的属性(在本例中为PointF类型)正确地序列化到窗体的designer.cs文件?

Update 更新资料

I'm now looking at a subclass of the CodeDomSerializer , which does change the designer.cs code (adding a comment as per the example on that page works) but it seems that I can only apply it to the CustomControl class as a whole, and try to modify the base serialization to replace the CodeCastExpression with a CodeObjectCreateExpression . 现在,我正在查看CodeDomSerializer的子类,它确实更改了designer.cs代码(按照该页面上的示例添加注释,但是似乎只能将其整体应用于CustomControl类,并尝试修改基本序列化,以将CodeCastExpression替换为CodeObjectCreateExpression This seems like a really messy way of doing things, though... 不过,这似乎是一种非常凌乱的处理方式。

Create the following class: 创建以下类:

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

Use the following class definition for your CustomControl : 对您的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;
        }
    }
}

Sources: 资料来源:

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

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