如何使用 spring4d 中的 IMultiCastEvent?
我尝试开始使用 spring4d 的集合部分。但我无法订阅集合更改事件。收到错误:[DCC Error]:E2008 不兼容类型位于:
var
TestList: TObjectList<TObject>;
begin
... List initialization code ...
TestList.OnNotify.Add(TestHandler); <--- Error here
end
TObjectList 的 OnNotify 属性声明为:
property OnNotify: ICollectionNotifyDelegate
,其中
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;
我很困惑%)请帮忙)
I'm try to start usage the collections part of a spring4d. But I cann't subscribe to the collection changing events. Get the error: [DCC Error]: E2008 Incompatible types at:
var
TestList: TObjectList<TObject>;
begin
... List initialization code ...
TestList.OnNotify.Add(TestHandler); <--- Error here
end
The OnNotify property of the TObjectList declared as:
property OnNotify: ICollectionNotifyDelegate<T>
, where
ICollectionNotifyDelegate<T> = interface(IMulticastEvent<Generics.Collections.TCollectionNotifyEvent<T>>)
end;
i.e. the OnNotify.Add method expects a Generics.Collections.TCollectionNotifyEvent, which declared as:
TCollectionNotifyEvent<T> = procedure(Sender: TObject; const Item: T;
Action: TCollectionNotification) of object;
my event handler declared as:
procedure TTestClass.TestHandler(Sender: TObject; const Item: TObject; Action: TCollectionNotification);
begin
end;
I'm confused %) please help )
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这是由于不同单元中的相同类型定义引起的:
Classes.pas:
TCollectionNotification = (cnAdded, cnExtracting, cnDeleting);
Generics.Collections.pas
TCollectionNotification = (cnAdded, cnRemoved, cnExtracted) ;
实际上,Spring.Collections 使用类型别名来简化使用:
TCollectionNotification = Generics.Collections.TCollectionNotification;
您可以在使用列表子句中的
Classes
之后添加Spring.Collections
。PS
建议使用接口版本
IList
。This was caused by the same type definitions in different units:
Classes.pas:
TCollectionNotification = (cnAdded, cnExtracting, cnDeleting);
Generics.Collections.pas
TCollectionNotification = (cnAdded, cnRemoved, cnExtracted);
Actually, Spring.Collections uses the type alias to simplify the uses:
TCollectionNotification = Generics.Collections.TCollectionNotification;
You can add
Spring.Collections
after theClasses
in your uses list clause.P.S.
It's recommended to use interfaced version
IList<T>
.