在运行时分配已发布属性的确切时间?

When exactly are published properties assigned at run time?

本文关键字:时间 属性 分配 运行时 布属性      更新时间:2023-10-16

我有一个自定义控件,该控件在运行时加载时需要根据已发布的属性执行一些操作。但是,我遇到了一个问题,每当我检查已发布的属性时,它还没有设置,并且始终是默认值。

我首先尝试检查控件的构造函数中的属性,但很快发现它们尚未加载。 我知道当控件显示在屏幕上时,属性设置正确,因此根本不加载属性的问题。

我接下来尝试覆盖加载的方法,但仍然遇到同样的问题,所以我认为这不是我正在寻找的。

void __fastcall TFmSearchBar::Loaded()
{
TEdit::Loaded(); //call base class loaded
if( MyProperty )
{
//do stuff
}
}   

这些已发布的属性实际上是在什么时候设置的?

一旦正确设置了属性,我可以/应该挂钩到什么方法,以便基于这些属性在我的控件中执行一些逻辑?

如果我在控件的构造函数中检查该属性,则该属性始终是默认值,即使我在设计器中另行指定也是如此。

正确,因为尚未分配其设计时值。

这些已发布的属性实际上是在什么时候设置的?

构造所有者(窗体、框架或数据模块)时。 它加载自己的 DFM 资源并对其进行解析,构造存储的子组件并读取其属性值。

例如,假设您有以下 DFM:

object Form1: TForm1
Left = 0
Top = 0
Caption = 'Form1'
...
object Edit1: TEdit
Left = 136
Top = 64
Width = 121
Height = 21
TabOrder = 0
end
object Button1: TButton
Left = 263
Top = 62
Width = 75
Height = 25
Caption = 'Button1'
TabOrder = 1
end
end

DFM 流式处理过程大致转换为以下等效代码(为简单起见,我省略了许多内部细节):

__fastcall TCustomForm::TCustomForm(TComponent *Owner)
: TScrollingWinControl(Owner)
{
this->FFormState << fsCreating;
try
{
// locate, load, and parse the "Form1" DFM resource ...
this->FComponentState << csLoading;
this->Parent = ...;
this->Name = L"Form1":
this->FComponentState << csReading;
this->Left = 0;
this->Top = 0;
this->Caption = L"Form1";
...
TEdit *e = new TEdit(this);
try
{
e->FComponentState << csLoading;
e->Parent = this;
e->Name = L"Edit1"; // <-- sets the derived Form's 'Edit1' member to this object
e->FComponentState << csReading;
e->Left = 136;
e->Top = 64;
e->Width = 121;
e->Height = 21;
e->TabOrder = 0;
e->FComponentState >> csReading;
}
catch (...)
{
delete e;
throw;
}
TButton *b = new TButton(this);
try
{
b->FComponentState << csLoading;
b->Parent = this;
b->Name = L"Button1"; // <-- sets the derived Form's 'Button1' member to this object
b->FComponentState << csReading;
b->Left = 263;
b->Top = 62;
b->Width = 75;
b->Height = 25;
b->Caption = L"Button1";
b->TabOrder = 1;
b->FComponentState >> csReading;
}
catch (...)
{
delete b;
throw;
}
this->FComponentState >> csReading;
...
e->Loaded();
b->Loaded();
this->Loaded();
}
__finally
{
this->FFormState >> fsCreating;
}
}

因此,如您所见,当调用组件的构造函数时,组件的属性值尚不可用。

一旦

正确设置了属性,我可以/应该挂钩到什么方法,以便基于这些属性在我的控件中执行一些逻辑?

这取决于属性需要做什么。 如果他们需要立即执行操作,您可以直接在其属性库中执行此操作。 但是,如果他们需要等到首先加载其他属性(如果一个属性依赖于另一个属性的值),则改为重写虚拟Loaded()方法,该方法将在 DFM 流式处理完成后自动调用。 属性资源库可以检查ComponentState属性的标志,以了解组件当前是否在设计时在窗体设计器中运行、当前是否正在流式传输 DFM 等,然后根据需要采取相应的操作。

我尝试覆盖加载的方法,但仍然遇到同样的问题

这到底是什么? 你没有解释你的实际问题是什么。 请编辑您的问题以提供这些详细信息。

所以我不认为这正是我想要的。

很可能是,您可能只是没有正确使用它。