如何使用通用TList的OnNotify

Arn*_*old 9 delphi generics delphi-xe5

我想使用通用TList的OnNotify事件.将过程分配给OnNotify会产生错误消息:

E2010 Incompatible types: 'System.Generics.Collections.TCollectionNotification' and 'System.Classes.TCollectionNotification'
Run Code Online (Sandbox Code Playgroud)

我正在声明一个类,并在其中使用通用TList如下:

TEditor_Table = class (TObject)
public
  FEditors: TList<TGradient_Editor>;  // List containing the editors
Run Code Online (Sandbox Code Playgroud)

这不是最好的方法,但我需要这个来进行测试.该列表在构造函数中实例化:

constructor TEditor_Table.Create (Owner: TFMXObject);
begin
   inherited Create;

   FEditors := TList<TGradient_Editor>.Create;
   FOwner := Owner;
end; // Create //
Run Code Online (Sandbox Code Playgroud)

接下来在main表单中声明一个函数

procedure do_editor_change (Sender: TObject; const Item: TGradient_Editor; Action: TCollectionNotification);
Run Code Online (Sandbox Code Playgroud)

并且TColor_Editor类实例化如下:

FColor_Editor := TEditor_Table.Create (List_Gradients);
FColor_Editor.FEditors.OnNotify := do_editor_change;
                                                   ^
error occurs here----------------------------------+
Run Code Online (Sandbox Code Playgroud)

我根本不理解这个消息,而且为什么编译器似乎混淆了两个单元:'System.Generics.Collections.TCollectionNotification'和'System.Classes.TCollectionNotification'.我究竟做错了什么?

Dav*_*nan 13

问题是RTL定义了两个不同的版本TCollectionNotification.一个在System.Classes一个在Generics.Collections.

您正在使用TList<T>Generics.Collections,因此需要TCollectionNotificationGenerics.Collections.但是在你的代码中TCollectionNotification是声明的版本System.Classes.那是因为,在你写作的那一点上TCollectionNotification,之后System.Classes被使用了Generics.Collections.

解决方案是:

  1. 更改您的使用顺序,以便Generics.Collections在之后显示System.Classes.无论如何,这都是好习惯.要么,
  2. 完全指定类型:Generics.Collections.TCollectionNotification.