delphi - Chrome : GetSourceProc and Incompatible types: 'regular procedure and method pointer'

标签 delphi chromium-embedded tchromium

我需要从 TChromium 中加载的页面获取源 HTML,但我需要将源存储在另一个类的变量中。换句话说,回调函数需要位于另一个类中,但我不能这样做,因为这个异常:

E2009 不兼容的类型:“常规过程和方法指针”

这是我的代码。仅当“StringVisitor”函数位于“Form1”类之外时它才有效。

有什么想法吗?

unit simple1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, cefvcl, ceflib;

type
  TForm1 = class(TForm)
    Chromium1: TChromium;
    procedure FormCreate(Sender: TObject);
    procedure Chromium1LoadEnd(Sender: TObject; const browser: ICefBrowser; const frame: ICefFrame;
      httpStatusCode: Integer);
  public
  mySource : string;
  procedure StringVisitor(const str: ustring);
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.Chromium1LoadEnd(Sender: TObject; const browser: ICefBrowser; const frame: ICefFrame;
  httpStatusCode: Integer);
begin
  Chromium1.Browser.MainFrame.GetSourceProc(StringVisitor);  // error on this line
end;

procedure TForm1.FormCreate(Sender: TObject);
begin
chromium1.load('http://www.google.com');
end;

procedure TForm1.StringVisitor(const str: ustring);
begin
mySource := str;
end;

end.

最佳答案

好的,使用 Delphi 7 我能够重现该问题。

深入研究问题后,我发现有两种不同的方法可以获取加载页面的源代码:

1) procedure GetSourceProc(const proc: TCefStringVisitorProc); 正如您所使用的。该解决方案的问题在于 Dlephi 2009 之前的版本中,它只接受独立的过程,当您有多个对象实例并希望在其中保留 HTML 时,这会给您带来问题。

2) 过程 GetSource(const Visitor: ICefStringVisitor);

让我们首先查看预期的界面

  ICefStringVisitor = interface(ICefBase)
    ['{63ED4D6C-2FC8-4537-964B-B84C008F6158}']
    procedure Visit(const str: ustring);
  end;

所以我开始研究这个问题。在挖掘 unit ceflib; 后,我发现了 TCefStringVisitorOwn,这就是您解决方案的关键。

TCefStringVisitorOwn 的界面非常简单:

  TCefStringVisitorOwn = class(TCefBaseOwn, ICefStringVisitor)
  protected
    procedure Visit(const str: ustring); virtual;
  public
    constructor Create; virtual;
  end;

因此,有了这个,我创建了 TCefStringVisitorOwn 的后代:

unit SourceContainerU;

interface

uses
  ceflib;

type
  ISourceContainer = interface(ICefStringVisitor)
    function Source: ustring;
  end;

  TSourceContainer = class(TCefStringVisitorOwn, ISourceContainer)
  private
    FSource: ustring;
  protected
    procedure Visit(const str: ustring); override;
  public
    function Source: ustring;
  end;

implementation

{ TSourceContainer }

function TSourceContainer.Source: ustring;
begin
  Result := FSource;
end;

procedure TSourceContainer.Visit(const str: ustring);
begin
  FSource := str;
end;

end.

这里没有什么魔法。

剩下的唯一一件事就是使用此类创建一个演示项目。在我的示例中,我在运行时创建 TChromium。该表单包含一个按钮和一个备忘录,然后是以下代码:

unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs, StdCtrls,
  cefvcl, ceflib, SourceContainerU;

type
  TForm1 = class(TForm)
    Button1: TButton;
    Memo1: TMemo;
    procedure FormCreate(Sender: TObject);
    procedure Button1Click(Sender: TObject);
  private
    { Private declarations }
    Chromium1: TChromium;
    SourceContainer : ISourceContainer;
    procedure Chromium1LoadEnd(Sender: TObject; const browser: ICefBrowser; const frame: ICefFrame;  httpStatusCode: Integer);
  public
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

{ TForm1 }

procedure TForm1.Chromium1LoadEnd(Sender: TObject;  const browser: ICefBrowser; const frame: ICefFrame;  httpStatusCode: Integer);
begin
  if Frame = nil then
    exit;

  SourceContainer := TSourceContainer.Create;
  Frame.GetSource(SourceContainer);
end;

procedure TForm1.FormCreate(Sender: TObject);
begin
  Chromium1 := TChromium.Create(self);
  Chromium1.Parent := self;
  Chromium1.OnLoadEnd := Chromium1LoadEnd;
  Chromium1.load('http://www.google.com');
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
  if Assigned(SourceContainer) then
    Memo1.Lines.Text := SourceContainer.Source;
end;

end.

此解决方案已使用最新版本的 Chromium 进行了测试,并且我已在 Delphi 7Delphi XEDelphi XE6 中进行了测试>。我没有安装任何 Delphi 2007,但我相信它也应该在 wotk 中工作。

关于delphi - Chrome : GetSourceProc and Incompatible types: 'regular procedure and method pointer' ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34440406/

相关文章:

delphi - Chrome : How to keep session alive

delphi - 使用 TChromium,Delphi Chromium 嵌入式

delphi - 要从一个对象复制到另一个对象,我可以直接分配变量,还是必须单独分配它们的属性?

Windows 7中Delphi复制文件到systemdir问题

c++ - Chromium Embedded 如何检测我们是否作为子进程运行?

delphi - 如何查看文本是否在浏览器中被选中,然后检索它?

delphi - 移动表单上的其他组件时更新自定义组件

forms - 禁用表单上升到顶部

c# - 如何将本地 HTML 加载到包含 CSS 样式表的 CefSharp?

delphi - 如何拥有最小尺寸的 Chromium Embedded Framework dll