将接口用作已发布的链接属性

时间:2016-11-14 08:48:26

标签: delphi properties interface components

我正在尝试发布一个属性,允许我在实现指定接口的所有组件之间进行选择。 有可能做这样的事吗?

我尝试使用接口作为已发布的属性,但它似乎不起作用。 以下是我遵循的步骤:

我已经定义了两个接口和三个用于测试的对象:

uses
  Classes, Variants;

type
  IMyInterfaceA = interface
    function FGetValue() : Variant;
  end;

  TMyObjectA = class(TComponent, IMyInterfaceA)
  protected
    FValue : Variant;
    FAlternativeValueSource : IMyInterfaceA;
    function FGetValue() : Variant;
  published
    property Value : Variant read FGetValue write FValue;
    property AlternativeValueSource : IMyInterfaceA read FAlternativeValueSource write FAlternativeValueSource;
  end;

  IMyInterfaceB = interface
    procedure DoSomething();
  end;

  TMyObjectB = class(TComponent, IMyInterfaceB)
  public
    procedure DoSomething();
  end;

  TMyObjectC = class(TComponent);

implementation

function TMyObjectA.FGetValue() : Variant;
begin
  if((FValue = Null) AND (FAlternativeValueSource <> nil))
  then Result := FAlternativeValueSource.FGetValue
  else Result := FValue;
end;

procedure TMyObjectB.DoSomething();
begin
  //do something
end;

然后我在设计时包中注册了TMyObjectATMyObjectBTMyObjectC

procedure Register();
begin
  RegisterComponents('MyTestComponents', [
    TMyObjectA,
    TMyObjectB,
    TMyObjectC
  ]);

  //requires DesignIDE, uses DesignIntf
  RegisterPropertyInCategory('Linkage', TMyObjectA, 'AlternativeValueSource');
end;

我在表单中添加了4个对象:

MyObjectA1: TMyObjectA;
MyObjectA2: TMyObjectA;
MyObjectB1: TMyObjectB;
MyObjectC1: TMyObjectC;

选择MyObjectA1,在对象检查器的AlternativeValueSource下拉列表中,我看到所有具有界面的对象。 (我期待只有MyObjectA1MyObjectA2实现IMyInterfaceA

enter image description here

1 个答案:

答案 0 :(得分:4)

定义接口的GUID。

  IMyInterfaceA = interface
  ['{A5675798-F457-4E32-B0AA-608717CFD242}']
    function FGetValue() : Variant;
  end;

Delphi的IDE从其GUID(设计时)识别接口。

相关问题