从 TNetHTTPClient 重载一个事件(私有方法不是虚拟的)
Overload an Event from TNetHTTPClient (private method not virtual)
我正在从 TNetHTTPClient 创建一个组件,我喜欢重载事件(在 TNetHTTPClient 的私有部分中声明的过程):
TNetHTTPClient = class(TComponent)
private
procedure DoOnRequestCompleted(const Sender: TObject; const AResponse: IHTTPResponse);
procedure DoOnRequestError(const Sender: TObject; const AError: string);
procedure DoOnReceiveData(const Sender: TObject; AContentLength: Int64; AReadCount: Int64; var Abort: Boolean);
但我不能从我的 class 中调用它们:像这样。
TRequestAccess = class(TNetHTTPClient)
private
procedure ShowLoadAnimation ;
procedure HideLoadAnimation ;
procedure DoOnRequestCompleted(const Sender: TObject; const AResponse: IHTTPResponse); override;
procedure DoOnRequestError(const Sender: TObject; const AError: string); override;
procedure DoOnReceiveData(const Sender: TObject; AContentLength: Int64; AReadCount: Int64; var Abort: Boolean); override;
...
procedure TRequestAccess.DoOnReceiveData(const Sender: TObject; AContentLength,
AReadCount: Int64; var Abort: Boolean);
begin
self.HideLoadAnimation ;
inherited;
end;
procedure TRequestAccess.DoOnRequestCompleted(const Sender: TObject;
const AResponse: IHTTPResponse);
begin
self.HideLoadAnimation ;
inherited;
end;
procedure TRequestAccess.DoOnRequestError(const Sender: TObject;
const AError: string);
begin
self.HideLoadAnimation ;
inherited;
end;
ShowLoadAnimation 和 HideLoadAnimation 是自动显示和隐藏 TAniIndicator 的 2 个函数。
似乎我无法重写,因为它是在私有和非虚拟中声明的?
我该怎么做?
我想我不能重写我自己的函数,因为有一个线程..
procedure TNetHTTPClient.DoOnRequestError(const Sender: TObject; const AError: string);
begin
if Assigned(FOnRequestError) then
TThread.Synchronize(nil, procedure
begin
FOnRequestError(Sender, AError);
end);
end;
这个工作正常。来自embarcadero的官方方法。
unit NetHTTPClientUnit;
interface
uses
System.SysUtils, System.Classes, System.Net.HttpClientComponent,System.Net.HttpClient,System.Net.URLClient;
type
TNewNetHTTPClient = class(TNetHTTPClient)
private
FOnAuthEventInternal : TCredentialsStorage.TCredentialAuthevent;
FOnReceiveDataInternal : TReceiveDataEvent;
FOnRequestCompletedInternal : TRequestCompletedEvent;
FOnRequestErrorInternal : TRequestErrorEvent;
function GetAuthEventOverride: TCredentialsStorage.TCredentialAuthevent;
procedure SetAuthEventOverride(const Value: TCredentialsStorage.TCredentialAuthevent);
function GetOnReceiveData: TReceiveDataEvent;
function GetOnRequestCompleted: TRequestCompletedEvent;
function GetOnRequestError: TRequestErrorEvent;
procedure SetOnReceiveData(const Value: TReceiveDataEvent);
procedure SetOnRequestCompleted(const Value: TRequestCompletedEvent);
procedure SetOnRequestError(const Value: TRequestErrorEvent);
{ Private declarations }
procedure InternalReceiveDataEvent(const Sender: TObject; AContentLength: Int64; AReadCount: Int64; var Abort: Boolean);
procedure InternalRequestErrorEvent(const Sender: TObject; const AError: string);
procedure InternalRequestCompletedEvent(const Sender: TObject; const AResponse: IHTTPResponse);
procedure InternalAuthEvent(const Sender: TObject;
AnAuthTarget: TAuthTargetType; const ARealm, AURL: string; var AUserName,
APassword: string; var AbortAuth: Boolean;
var Persistence: TAuthPersistenceType);
protected
{ Protected declarations }
public
constructor Create(AOwner: TComponent); override;
published
/// <summary> UserName needed to be authenticated to the proxy</summary>
property OnAuthEvent: TCredentialsStorage.TCredentialAuthevent read GetAuthEventOverride write SetAuthEventOverride;
/// <summary> Event fired when a request finishes</summary>
property OnRequestCompleted: TRequestCompletedEvent read GetOnRequestCompleted write SetOnRequestCompleted;
/// <summary> Event fired when a request has an error</summary>
property OnRequestError: TRequestErrorEvent read GetOnRequestError write SetOnRequestError;
/// <summary>Property to manage the ReceiveData Event</summary>
property OnReceiveData: TReceiveDataEvent read GetOnReceiveData write SetOnReceiveData;
end;
procedure Register;
implementation
Uses WinApi.Windows;
procedure Register;
begin
RegisterComponents('Net', [TNewNetHTTPClient]);
end;
{ TNewNetHTTPClient }
constructor TNewNetHTTPClient.Create(AOwner: TComponent);
begin
inherited;
inherited OnAuthEvent := InternalAuthEvent;
inherited OnRequestCompleted := InternalRequestCompletedEvent;
inherited OnRequestError := InternalRequestErrorEvent;
inherited OnReceiveData := FOnReceiveDataInternal;
end;
function TNewNetHTTPClient.GetAuthEventOverride: TCredentialsStorage.TCredentialAuthevent;
begin
Result := FOnAuthEventInternal;
end;
function TNewNetHTTPClient.GetOnReceiveData: TReceiveDataEvent;
begin
result := FOnReceiveDataInternal;
end;
function TNewNetHTTPClient.GetOnRequestCompleted: TRequestCompletedEvent;
begin
Result := FOnRequestCompletedInternal;
end;
function TNewNetHTTPClient.GetOnRequestError: TRequestErrorEvent;
begin
Result := FOnRequestErrorInternal;
end;
procedure TNewNetHTTPClient.InternalAuthEvent(const Sender: TObject;
AnAuthTarget: TAuthTargetType; const ARealm, AURL: string; var AUserName,
APassword: string; var AbortAuth: Boolean;
var Persistence: TAuthPersistenceType);
begin
OutputDebugString('InternalAuthEvent');
if Assigned(FOnAuthEventInternal) then
FOnAuthEventInternal(Sender, AnAuthTarget, ARealm, AURL,AUserName,APassword,AbortAuth,Persistence);
end;
procedure TNewNetHTTPClient.InternalReceiveDataEvent(const Sender: TObject;
AContentLength, AReadCount: Int64; var Abort: Boolean);
begin
OutputDebugString('InternalReceiveDataEvent');
if Assigned(FOnReceiveDataInternal) then
FOnReceiveDataInternal(Sender, AContentLength, AReadCount, Abort);
end;
procedure TNewNetHTTPClient.InternalRequestCompletedEvent(const Sender: TObject;
const AResponse: IHTTPResponse);
begin
OutputDebugString('RequestCompletedEvent');
if AResponse <> nil then
OutputDebugString( PChar(AResponse.ContentEncoding));
if Assigned(FOnRequestCompletedInternal) then
FOnRequestCompletedInternal(Sender,AResponse);
end;
procedure TNewNetHTTPClient.InternalRequestErrorEvent(const Sender: TObject;
const AError: string);
begin
OutputDebugString(PChar('RequestErrorEvent ' + AError));
if Assigned(FOnRequestErrorInternal) then
FOnRequestErrorInternal(Sender,AError);
end;
procedure TNewNetHTTPClient.SetAuthEventOverride(
const Value: TCredentialsStorage.TCredentialAuthevent);
begin
FOnAuthEventInternal := Value;
end;
procedure TNewNetHTTPClient.SetOnReceiveData(const Value: TReceiveDataEvent);
begin
FOnReceiveDataInternal := Value;
end;
procedure TNewNetHTTPClient.SetOnRequestCompleted(
const Value: TRequestCompletedEvent);
begin
FOnRequestCompletedInternal := Value;
end;
procedure TNewNetHTTPClient.SetOnRequestError(const Value: TRequestErrorEvent);
begin
FOnRequestErrorInternal := Value;
end;
end.
我正在从 TNetHTTPClient 创建一个组件,我喜欢重载事件(在 TNetHTTPClient 的私有部分中声明的过程):
TNetHTTPClient = class(TComponent)
private
procedure DoOnRequestCompleted(const Sender: TObject; const AResponse: IHTTPResponse);
procedure DoOnRequestError(const Sender: TObject; const AError: string);
procedure DoOnReceiveData(const Sender: TObject; AContentLength: Int64; AReadCount: Int64; var Abort: Boolean);
但我不能从我的 class 中调用它们:像这样。
TRequestAccess = class(TNetHTTPClient)
private
procedure ShowLoadAnimation ;
procedure HideLoadAnimation ;
procedure DoOnRequestCompleted(const Sender: TObject; const AResponse: IHTTPResponse); override;
procedure DoOnRequestError(const Sender: TObject; const AError: string); override;
procedure DoOnReceiveData(const Sender: TObject; AContentLength: Int64; AReadCount: Int64; var Abort: Boolean); override;
...
procedure TRequestAccess.DoOnReceiveData(const Sender: TObject; AContentLength,
AReadCount: Int64; var Abort: Boolean);
begin
self.HideLoadAnimation ;
inherited;
end;
procedure TRequestAccess.DoOnRequestCompleted(const Sender: TObject;
const AResponse: IHTTPResponse);
begin
self.HideLoadAnimation ;
inherited;
end;
procedure TRequestAccess.DoOnRequestError(const Sender: TObject;
const AError: string);
begin
self.HideLoadAnimation ;
inherited;
end;
ShowLoadAnimation 和 HideLoadAnimation 是自动显示和隐藏 TAniIndicator 的 2 个函数。
似乎我无法重写,因为它是在私有和非虚拟中声明的?
我该怎么做?
我想我不能重写我自己的函数,因为有一个线程..
procedure TNetHTTPClient.DoOnRequestError(const Sender: TObject; const AError: string);
begin
if Assigned(FOnRequestError) then
TThread.Synchronize(nil, procedure
begin
FOnRequestError(Sender, AError);
end);
end;
这个工作正常。来自embarcadero的官方方法。
unit NetHTTPClientUnit;
interface
uses
System.SysUtils, System.Classes, System.Net.HttpClientComponent,System.Net.HttpClient,System.Net.URLClient;
type
TNewNetHTTPClient = class(TNetHTTPClient)
private
FOnAuthEventInternal : TCredentialsStorage.TCredentialAuthevent;
FOnReceiveDataInternal : TReceiveDataEvent;
FOnRequestCompletedInternal : TRequestCompletedEvent;
FOnRequestErrorInternal : TRequestErrorEvent;
function GetAuthEventOverride: TCredentialsStorage.TCredentialAuthevent;
procedure SetAuthEventOverride(const Value: TCredentialsStorage.TCredentialAuthevent);
function GetOnReceiveData: TReceiveDataEvent;
function GetOnRequestCompleted: TRequestCompletedEvent;
function GetOnRequestError: TRequestErrorEvent;
procedure SetOnReceiveData(const Value: TReceiveDataEvent);
procedure SetOnRequestCompleted(const Value: TRequestCompletedEvent);
procedure SetOnRequestError(const Value: TRequestErrorEvent);
{ Private declarations }
procedure InternalReceiveDataEvent(const Sender: TObject; AContentLength: Int64; AReadCount: Int64; var Abort: Boolean);
procedure InternalRequestErrorEvent(const Sender: TObject; const AError: string);
procedure InternalRequestCompletedEvent(const Sender: TObject; const AResponse: IHTTPResponse);
procedure InternalAuthEvent(const Sender: TObject;
AnAuthTarget: TAuthTargetType; const ARealm, AURL: string; var AUserName,
APassword: string; var AbortAuth: Boolean;
var Persistence: TAuthPersistenceType);
protected
{ Protected declarations }
public
constructor Create(AOwner: TComponent); override;
published
/// <summary> UserName needed to be authenticated to the proxy</summary>
property OnAuthEvent: TCredentialsStorage.TCredentialAuthevent read GetAuthEventOverride write SetAuthEventOverride;
/// <summary> Event fired when a request finishes</summary>
property OnRequestCompleted: TRequestCompletedEvent read GetOnRequestCompleted write SetOnRequestCompleted;
/// <summary> Event fired when a request has an error</summary>
property OnRequestError: TRequestErrorEvent read GetOnRequestError write SetOnRequestError;
/// <summary>Property to manage the ReceiveData Event</summary>
property OnReceiveData: TReceiveDataEvent read GetOnReceiveData write SetOnReceiveData;
end;
procedure Register;
implementation
Uses WinApi.Windows;
procedure Register;
begin
RegisterComponents('Net', [TNewNetHTTPClient]);
end;
{ TNewNetHTTPClient }
constructor TNewNetHTTPClient.Create(AOwner: TComponent);
begin
inherited;
inherited OnAuthEvent := InternalAuthEvent;
inherited OnRequestCompleted := InternalRequestCompletedEvent;
inherited OnRequestError := InternalRequestErrorEvent;
inherited OnReceiveData := FOnReceiveDataInternal;
end;
function TNewNetHTTPClient.GetAuthEventOverride: TCredentialsStorage.TCredentialAuthevent;
begin
Result := FOnAuthEventInternal;
end;
function TNewNetHTTPClient.GetOnReceiveData: TReceiveDataEvent;
begin
result := FOnReceiveDataInternal;
end;
function TNewNetHTTPClient.GetOnRequestCompleted: TRequestCompletedEvent;
begin
Result := FOnRequestCompletedInternal;
end;
function TNewNetHTTPClient.GetOnRequestError: TRequestErrorEvent;
begin
Result := FOnRequestErrorInternal;
end;
procedure TNewNetHTTPClient.InternalAuthEvent(const Sender: TObject;
AnAuthTarget: TAuthTargetType; const ARealm, AURL: string; var AUserName,
APassword: string; var AbortAuth: Boolean;
var Persistence: TAuthPersistenceType);
begin
OutputDebugString('InternalAuthEvent');
if Assigned(FOnAuthEventInternal) then
FOnAuthEventInternal(Sender, AnAuthTarget, ARealm, AURL,AUserName,APassword,AbortAuth,Persistence);
end;
procedure TNewNetHTTPClient.InternalReceiveDataEvent(const Sender: TObject;
AContentLength, AReadCount: Int64; var Abort: Boolean);
begin
OutputDebugString('InternalReceiveDataEvent');
if Assigned(FOnReceiveDataInternal) then
FOnReceiveDataInternal(Sender, AContentLength, AReadCount, Abort);
end;
procedure TNewNetHTTPClient.InternalRequestCompletedEvent(const Sender: TObject;
const AResponse: IHTTPResponse);
begin
OutputDebugString('RequestCompletedEvent');
if AResponse <> nil then
OutputDebugString( PChar(AResponse.ContentEncoding));
if Assigned(FOnRequestCompletedInternal) then
FOnRequestCompletedInternal(Sender,AResponse);
end;
procedure TNewNetHTTPClient.InternalRequestErrorEvent(const Sender: TObject;
const AError: string);
begin
OutputDebugString(PChar('RequestErrorEvent ' + AError));
if Assigned(FOnRequestErrorInternal) then
FOnRequestErrorInternal(Sender,AError);
end;
procedure TNewNetHTTPClient.SetAuthEventOverride(
const Value: TCredentialsStorage.TCredentialAuthevent);
begin
FOnAuthEventInternal := Value;
end;
procedure TNewNetHTTPClient.SetOnReceiveData(const Value: TReceiveDataEvent);
begin
FOnReceiveDataInternal := Value;
end;
procedure TNewNetHTTPClient.SetOnRequestCompleted(
const Value: TRequestCompletedEvent);
begin
FOnRequestCompletedInternal := Value;
end;
procedure TNewNetHTTPClient.SetOnRequestError(const Value: TRequestErrorEvent);
begin
FOnRequestErrorInternal := Value;
end;
end.