【问题标题】:TPersistent: NotificationTPersistent:通知
【发布时间】:2012-12-15 09:16:14
【问题描述】:

我有一个 TPersistent 定义如下:

  TGlyph = class(TPersistent)
  private
    FOwner: TControl;
    FLayout: TGlyphAlignment;
    FVisible: Boolean;
    FImageIndex: Integer;
    FImages: TImageList;
    ..............
  protected
    procedure Invalidate;
  public
    constructor Create(AOwner: TControl);
    destructor Destroy; override;
    .............
  published
    property ImageIndex: Integer read FImageIndex write SetImageIndex default -1;
    property Images: TImageList read FImages write SetImages;
    .............
  end;

是否必须有一个将 nil 值分配给 FImages 字段的通知程序,例如您用于 TComponent 的那种?

procedure TGlyph.Notification(AComponent: TComponent; Operation: TOperation);
begin
  inherited;
  if (Operation = opRemove) and (AComponent = FImages) then
  begin
    FImages.OnChange := nil;
    FImages := nil;
    Invalidate;
  end;
end; 

如果有,这个程序应该怎么写?

谢谢你, 恩佐

【问题讨论】:

    标签: delphi components delphi-xe3


    【解决方案1】:

    TPersistent 不支持Notification() 系统。为此,您需要TComponent。如果TGlyph 在您也编写的TComponent 内使用,那么您可以让TComponent 处理通知并在需要时更新TGlyph。否则,您将不得不将TGlyph 更改为从TComponent 派生,在这种情况下,如果TGlyphTComponent 中使用,那么请确保TGlyph 在其自身上调用SetSubComponent(True) 以避免任何对象检查器和 DFM 流问题。

    【讨论】:

    • 非常感谢您的快速答复。我非常感谢您的第一个解决方案,该解决方案刚刚实施并且运行良好!
    【解决方案2】:

    这取决于你的类是如何使用的。 Notification 方法不会自动调用,只能由您自己的代码(或您的类用户编写的代码)调用。因此,如果从未调用过Notification,则不需要。

    【讨论】:

      猜你喜欢
      • 2010-11-28
      • 1970-01-01
      • 2012-07-02
      • 1970-01-01
      • 1970-01-01
      • 2012-07-10
      • 2018-03-25
      • 2019-08-21
      • 2017-10-24
      相关资源
      最近更新 更多