如何将方法的嵌套过程作为参数传递?

Thi*_*ien 1 delphi freepascal delphi-2010

如果TForm有一个TListBox上面,下面的工作:

procedure TForm1.FormCreate(Sender: TObject);
  procedure _WorkOnListBox;
  begin
    ListBox.Items.Append('Test');
  end;
begin  
  _WorkOnListBox;
end;
Run Code Online (Sandbox Code Playgroud)

如下所示:

procedure TForm1.DoWithoutListBoxEvents(AProc: TProc);
begin
  ListBox.Items.BeginUpdate;
  try
    AProc;  
  finally
    ListBox.Items.EndUpdate;
  end;
end;

procedure TForm1.FormCreate(Sender: TObject);
begin
  DoWithoutListBoxEvents(procedure
  begin
    LayersListBox.Items.Append('Test'); 
  end);
end;
Run Code Online (Sandbox Code Playgroud)

但以下不是:

procedure TForm1.FormCreate(Sender: TObject);
  procedure _WorkOnListBox;
  begin
    ListBox.Items.Append('Test');
  end;
begin  
  DoWithoutListBoxEvents(_WorkOnListBox);
end;
Run Code Online (Sandbox Code Playgroud)

我得到了E2555 Cannot capture symbol '_WorkOnListBox'.为什么?有没有办法在DoWithoutListBoxEvents不使用匿名程序的情况下开始工作?虽然我认为它看起来很优雅,但我试图保持FPC兼容.

Rem*_*eau 5

DoWithoutEvents()需要TProc作为输入:

type
  TProc = procedure;
Run Code Online (Sandbox Code Playgroud)

只能将一个独立的非类过程和一个匿名过程分配给a TProc. _WorkOnForm不是那些,而是一个本地程序.本地过程具有特殊的编译器处理功能,可将其与父代的堆栈帧相关联.因此,_WorkOnForm与之不相容TProc.