我创建了一个自定义组件TCustomHTTPReqResp,继承自THTTPReqResp。
我还为这个组件创建了一个自定义事件。唯一的问题是,虽然该事件已发布并出现在IDE中,但当我分配事件处理程序并运行应用程序时,事件处理程序不会被调用。
但是如果在Form.Create代码中分配它,例如:
CustomHTTPReqResp1.OnBeforeGet := CustomHTTPReqResp1BeforeGet;
它起作用了。除此之外,其他所有内容都正常工作。
我做错了什么吗?提前感谢。
这是自定义组件的代码:
unit CCustomHTTPReqResp;
interface
uses
SysUtils, Classes, Dialogs, SOAPHTTPTrans;
type
TCustomHTTPReqResp = class(THTTPReqResp)
private
{ Private declarations }
FOnBeforeGet: TNotifyEvent;
procedure DoOnBeforeGet;
protected
{ Protected declarations }
procedure SetOnBeforeGet(const AOnBeforeGet: TNotifyEvent);
public
{ Public declarations }
constructor Create(Owner: TComponent); override;
destructor Destroy; override;
procedure Get(Resp: TStream); override;
published
{ Published declarations }
{ Events }
property OnBeforeGet: TNotifyEvent read FOnBeforeGet write SetOnBeforeGet;
end;
procedure Register;
implementation
procedure Register;
begin
RegisterComponents('My Components', [TCustomHTTPReqResp]);
end;
{ TCustomHTTPReqResp }
constructor TCustomHTTPReqResp.Create(Owner: TComponent);
begin
inherited Create(Owner);
// Code here.
end;
destructor TCustomHTTPReqResp.Destroy;
begin
// Code here.
inherited;
end;
procedure TCustomHTTPReqResp.SetOnBeforeGet(const AOnBeforeGet: TNotifyEvent);
begin
FOnBeforeGet := AOnBeforeGet;
end;
procedure TCustomHTTPReqResp.DoOnBeforeGet;
begin
if Assigned(FOnBeforeGet) then
begin
FOnBeforeGet(Self);
end
else
begin
MessageDlg('No Before Post Event Handler found!', mtInformation, mbOKCancel, 0);
end;
end;
procedure TCustomHTTPReqResp.Get(Resp: TStream);
begin
// Raise OnBeforeGet.
DoOnBeforeGet;
inherited Get(Resp);
end;
end.
FOnBeforeGet
编写setter,因此可以省略SetOnBeforeGet
并直接使用property OnBeforeGet: TNotifyEvent read FOnBeforeGet write FOnBeforeGet;
。 - TLama