【问题标题】:Use an interface as published linkage property使用接口作为发布的链接属性
【发布时间】:2017-03-27 20:36:25
【问题描述】:

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

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

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

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

【问题讨论】:

    标签: delphi properties interface components


    【解决方案1】:

    为您的界面定义一个 GUID。

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

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

    【讨论】:

    • 添加 GUID 后,一切都按要求开始工作。谢谢!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2022-01-22
    • 1970-01-01
    • 2012-03-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-04-03
    相关资源
    最近更新 更多