【问题标题】:How to use a IMultiCastEvent from the spring4d?如何使用 spring4d 中的 IMultiCastEvent?
【发布时间】:2012-02-05 15:17:03
【问题描述】:

我正在尝试开始使用 spring4d 的集合部分。但我无法订阅集合更改事件。得到错误:[DCC Error]: E2008 Incompatible types at:

var
  TestList: TObjectList<TObject>;
begin
  ... List initialization code ...

  TestList.OnNotify.Add(TestHandler);     <--- Error here
end

TObjectList 的 OnNotify 属性声明为:

property OnNotify: ICollectionNotifyDelegate&lt;T&gt;,在哪里

ICollectionNotifyDelegate<T> = interface(IMulticastEvent<Generics.Collections.TCollectionNotifyEvent<T>>)
end;

即OnNotify.Add 方法需要一个 Generics.Collections.TCollectionNotifyEvent,它声明为:

TCollectionNotifyEvent<T> = procedure(Sender: TObject; const Item: T; 
    Action: TCollectionNotification) of object;

我的事件处理程序声明为:

procedure TTestClass.TestHandler(Sender: TObject; const Item: TObject; Action: TCollectionNotification);
begin

end;

我很困惑 %) 请帮忙)

【问题讨论】:

    标签: delphi spring4d


    【解决方案1】:

    这是由于不同单元中的相同类型定义造成的:

    Classes.pas:

    TCollectionNotification = (cnAdded, cnExtracting, cnDeleting);

    Generics.Collections.pas

    TCollectionNotification = (cnAdded, cnRemoved, cnExtracted);

    实际上,Spring.Collections 使用类型别名来简化使用:

    TCollectionNotification = Generics.Collections.TCollectionNotification;

    您可以在使用列表子句中的Classes 之后添加Spring.Collections

    附:

    推荐使用接口版IList&lt;T&gt;

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-10-27
      • 1970-01-01
      • 1970-01-01
      • 2017-10-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多