【问题标题】:Delphi Component Custom property not to be saved on the DFM filesDelphi 组件自定义属性不保存在 DFM 文件中
【发布时间】:2021-01-17 03:17:34
【问题描述】:

我在自定义组件上有一个属性,我不想将其保存在 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 文件?

谢谢。

【问题讨论】:

标签: delphi delphi-10.4-sydney


【解决方案1】:

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

 property CommandTimeout: integer read GetCommandTimeout write SetCommandTimeout stored False;

参考:Properties (Delphi) -> Storage Specifiers

【讨论】:

    【解决方案2】:

    防止属性保存到 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 是没有意义的。如果您只是尝试移植一些旧代码,则不需要为旧属性添加设计时支持。


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

    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 对象。

    【讨论】:

      猜你喜欢
      • 2012-12-30
      • 1970-01-01
      • 2012-07-17
      • 2014-01-06
      • 1970-01-01
      • 2020-09-27
      • 2010-11-23
      • 2010-09-21
      • 2011-05-02
      相关资源
      最近更新 更多