4

我不想保存在 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 文件?

谢谢你。

4

2 回答 2

9

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

 property CommandTimeout: integer read GetCommandTimeout write SetCommandTimeout stored False;

参考:属性(Delphi)-> 存储说明符

于 2020-10-01T11:56:58.000 回答
6

防止属性保存到 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属性,则在设计时将其公开给 Object Inspector 是没有意义的。如果您只是想移植一些旧代码,那么您不需要为旧属性添加设计时支持。


话虽如此,为了移植旧代码,请考虑使用类助手而不是派生完整组件,例如:

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

于 2020-10-01T16:53:42.300 回答