Delphi组件自定义属性不保存在DFM文件上



我有一个自定义组件的属性,我不想保存在DFM文件中。我高估了DefineProperties方法,不提供ReadData和WriteData过程,期望它不会保存其值,但它仍然保存。

procedure TAEFDQuery.DefineProperties(Filer: TFiler);
begin
inherited;
// Set the ADO Compatibility custom properties to not be saved on the DFM
Filer.DefineProperty('CommandText', nil, nil, False);
end;

不保存此属性的原因是因为我已经将一个项目从ADO移植到FireDAC;假的";属性,允许某些ADO代码在不变的情况下运行,并将其重定向到相应的FireDAC属性。

type
TAEFDQuery = class(TFDQuery)
private
function GetCommandText: string;
procedure SetCommandText(AValue: string);
protected
procedure DefineProperties(Filer: TFiler); override;
published
property CommandText: integer read GetCommandText write SetCommandText;
end;
implementation
procedure TAEFDQuery.SetCommandText(AValue: string);
begin
SQL.Text := AValue;
end;
function TAEFDQuery.GetCommandText: string;
begin
Result := SQL.Text;
end;
procedure TAEFDQuery.DefineProperties(Filer: TFiler);
begin
inherited;
// Set the ADO Compatibility custom properties to not be saved on the DFM
Filer.DefineProperty('CommandText', nil, nil, False);
end;

如何正确地保持这些";假的";属性,而不让它们用实际属性的无用副本填充DFM文件?。

谢谢。

向为或返回false的属性添加存储说明符。

property CommandTimeout: integer read GetCommandTimeout write SetCommandTimeout stored False;

ref:属性(Delphi(->存储规范

防止属性保存到DFM的另一种方法是简单地将属性声明为public而不是published,因为只有published属性流入/流出DFM。
type
TAEFDQuery = class(TFDQuery)
private
function GetCommandText: string;
procedure SetCommandText(AValue: string);
public
property CommandText: integer read GetCommandText write SetCommandText;
end;

如果无法保存published属性,那么在设计时将其公开给对象检查器是没有意义的。如果您只是试图移植一些旧代码,那么您不需要为旧属性添加设计时支持。


也就是说,为了移植旧代码,考虑使用类助手而不是派生完整的组件,例如:

unit MyFDQueryHelper;
interface
uses
FireDAC.Comp.Client;
type
TAEFDQuery = class helper for TFDQuery
private
function GetCommandText: string;
procedure SetCommandText(AValue: string);
public
property CommandText: integer read GetCommandText write SetCommandText;
end;
implementation
procedure TAEFDQuery.SetCommandText(AValue: string);
begin
Self.SQL.Text := AValue;
end;
function TAEFDQuery.GetCommandText: string;
begin
Result := Self.SQL.Text;
end;
end.

现在,您只需将MyFDQueryHelper添加到单元的uses子句中,该单元中的任何TFDQuery对象都将自动获得新的CommandText属性。无需将TFDQuery对象替换为TAEFDQuery对象。

最新更新