简体   繁体   English

在设计时重置已发布的属性默认值

[英]Published properties defaults reset in Design-time

I'm developing some components - custom buttons.我正在开发一些组件 - 自定义按钮。 I installed it, but in design-time custom published properties resets to zeros.我安装了它,但在设计时自定义发布的属性重置为零。 First of all I'm talking about colors - it resets to clBlack (or for clBtnFace for Color property).首先,我在谈论 colors - 它重置为clBlack (或用于Color属性的clBtnFace )。 Caption reset to empty string. Caption重置为空字符串。 I mean when I drop component to form in design-time all custom properties in Object Inspector reset to zero (colors to clBlack and so on).我的意思是当我在设计时放置组件以形成 Object 检查器中的所有自定义属性时重置为零(颜色为clBlack等等)。 I can change it manually but why don't work default values that I set in code?我可以手动更改它,但为什么不使用我在代码中设置的默认值? The problem is only in design-time.问题仅存在于设计时。 When I create component in run-time it works fine.当我在运行时创建组件时,它工作正常。 Here is code (take for example Color property).这是代码(以Color属性为例)。

Base class底座 class

TpfCustomButton = class(TCustomControl)
...
published
...
  property Color;
...
end;

Main code主要代码

TpfCustomColoredButton = class(TpfCustomButton)
...
public
  constructor Create(AOwner: TComponent);
...
end;

constructor TpfCustomColoredButton.Create(AOwner: TComponent);
begin
  inherited Create(AOwner);

  Color := $00E1E1E1;//Look at this: setting Color
  ...
end;

Component Code组件代码

TpfColoredButton = class(TpfCustomColoredButton)
published
  ...
  property Action;
  property Align;
  //And some other standard properties
end;

procedure Register;

implementation

procedure Register;
begin
  RegisterComponents('PF Components', [TpfColoredButton]);
end;
...

Furthermore, just for test I'm trying so code:此外,只是为了测试我正在尝试这样的代码:

TpfColoredButton = class(TpfCustomColoredButton)
  public
    constructor Create(AOwner: TComponent);
  ...  
  
constructor TpfColoredButton.Create(AOwner: TComponent);
begin
  inherited Create(AOwner);
  Caption := 'abc';
end;

And in design-time Caption was empty, but again, if I create it in run-time we see the Caption=abc as we expect.在设计时Caption是空的,但同样,如果我在运行时创建它,我们会看到Caption=abc正如我们所料。 In run-time I create new object like this (and it works OK):在运行时,我像这样创建新的 object(它工作正常):

TForm2 = class(TForm)  
  procedure FormCreate(Sender: TObject);
private
  pf: TpfColoredButton;
end;

procedure TForm2.FormCreate(Sender: TObject);
begin
  pf := TpfColoredButton.Create(Self);
  pf.Parent := Self;
end;

You are changing the default value of the property in your derived class constructor, but you are not specifying the same default value in the property declaration to update its RTTI, which is used by the Object Inspector and DFM streaming.您正在派生的 class 构造函数中更改属性的默认值,但您没有在属性声明中指定相同的默认值来更新其 RTTI,Object Inspector 和 DFM 流使用该默认值。

Also, you are missing override on your derived constructors.此外,您缺少派生构造函数的override This is why your properties are not getting initialized properly when creating the components at design-time.这就是为什么在设计时创建组件时您的属性没有正确初始化的原因。 Your derived constructors are not even being called.您的派生构造函数甚至没有被调用。 Whereas at run-time, you are calling the derived constructors directly.而在运行时,您直接调用派生构造函数。

Change this:改变这个:

TpfCustomColoredButton = class(TpfCustomButton)
  ...
public
  constructor Create(AOwner: TComponent);
  ...
end;

To this:对此:

TpfCustomColoredButton = class(TpfCustomButton)
  ...
published
  ...
  property Color default $00E1E1E1;
  ...
public
  constructor Create(AOwner: TComponent); override;
  ...
end;

Do the same with all other published properties of derived classes that have different default values than their base classes.对具有与其基类不同的默认值的派生类的所有其他已发布属性执行相同操作。

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

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