0

我正在将应用程序从 TWebBrowser 移植到 TEdgeBrowser。我正在使用来自 Nuget 站点的最新 WebView2Loader,并且在可追溯到 2021 年 1 月的每个版本中,所有版本的 WebView2Loader 都存在相同的一致问题。

TEdgeBrowser 运行良好,但任何页面的首次加载可能需要 14 秒到 24 秒之间的任何时间,通常在 14-18 秒之间。加载第一页后,任何后续页面加载的执行速度都与您在完整浏览器中的预期一样快。

我指定了一个 UserDataFolder 和一个 BrowserExecutableFolder,以及 OnCreateWebViewCompleted、OnNavigationStarting 和 OnNavigationCompleted 事件。

我检查了界面代码中定义的任何错误常量,但返回的唯一错误是“COREWEBVIEW2_WEB_ERROR_STATUS_UNKNOWN”,每次我成功导航到网页时都会发生这种错误。

有没有人对可能导致这种延迟的原因有任何见解?我可以在 Microsft Edge 旁边运行我的嵌入式浏览器,并且 Edge 加载速度按预期快速,但 TEdgeBrowser 总是有初始延迟。

任何帮助将不胜感激。 在此处输入图像描述

unit main;

interface

uses
  Winapi.Windows,
  Winapi.Messages,
  System.SysUtils,
  System.Variants,
  System.Classes,
  Vcl.Graphics,
  Vcl.Controls,
  Vcl.Forms,
  Vcl.Dialogs,
  Winapi.WebView2,
  Winapi.ActiveX,
  Vcl.Edge,
  Vcl.StdCtrls,
  Vcl.ExtCtrls,
  Vcl.OleCtrls,
  Vcl.ComCtrls,
  Vcl.Mask;

type
  TForm2 = class(TForm)
    Panel1: TPanel;
    Button1: TButton;
    Edit1: TEdit;
    moLog: TMemo;
    Splitter1: TSplitter;
    edNavElapsedTime: TLabeledEdit;
    PageControl1: TPageControl;
    tsEB: TTabSheet;
    procedure Button1Click(Sender: TObject);
    procedure FormCreate(Sender: TObject);
  private
    FStart: TDateTime;
    FStop: TDateTime;
    FEB: TEdgeBrowser;
    procedure CreateWebViewCompleted(Sender: TCustomEdgeBrowser; AResult: HResult);
    procedure NavigationCompleted(Sender: TCustomEdgeBrowser; IsSuccess: Boolean; WebErrorStatus: COREWEBVIEW2_WEB_ERROR_STATUS);
    procedure NavigationStarting(Sender: TCustomEdgeBrowser; Args: TNavigationStartingEventArgs);
  end;

var
  Form2: TForm2;

implementation

{$R *.dfm}

uses
  System.DateUtils;

procedure LogEntry(ALogEntry: UnicodeString);
begin
  Form2.moLog.Lines.Insert(0,'[' + DateTimeToStr(Now) + ']: ' + ALogEntry);
end;

procedure TForm2.FormCreate(Sender: TObject);
var
  sUserCacheFolder,sExeFolder: UnicodeString;
begin
  sExeFolder := ExtractFileDir(Application.ExeName);
  sUserCacheFolder := sExeFolder + '\edgecache';
  if not ForceDirectories(sUserCacheFolder) then
    raise Exception.Create('Error: Failed to create Edge Browser user cache folder. Check directory file access rights.');

  FEB := TEdgeBrowser.Create(tsEB);
  FEB.Parent := tsEB;
  FEB.Align := alClient;
  if not FEB.WebViewCreated then
    FEB.CreateWebView;
  FEB.OnCreateWebViewCompleted := CreateWebViewCompleted;
  FEB.OnNavigationStarting := NavigationStarting;
  FEB.OnNavigationCompleted := NavigationCompleted;
  FEB.UserDataFolder := sUserCacheFolder;
  FEB.BrowserExecutableFolder := sExeFolder;
end;

procedure TForm2.NavigationCompleted(Sender: TCustomEdgeBrowser;     IsSuccess: Boolean; WebErrorStatus: COREWEBVIEW2_WEB_ERROR_STATUS);
var
  sErr: UnicodeString;
  yr,mon,day,hr,min,sec,msec: Word;
begin
  if IsSuccess then
    LogEntry('Navigation completed successfully');
  sErr := '';
  case WebErrorStatus of
//    COREWEBVIEW2_WEB_ERROR_STATUS_UNKNOWN: sErr := 'UNKNOWN'; // seems to be useless
    COREWEBVIEW2_WEB_ERROR_STATUS_CERTIFICATE_COMMON_NAME_IS_INCORRECT: sErr := 'CERTIFICATE_COMMON_NAME_IS_INCORRECT';
    COREWEBVIEW2_WEB_ERROR_STATUS_CERTIFICATE_EXPIRED: sErr := 'CERTIFICATE_EXPIRED';
    COREWEBVIEW2_WEB_ERROR_STATUS_CLIENT_CERTIFICATE_CONTAINS_ERRORS: sErr := 'CLIENT_CERTIFICATE_CONTAINS_ERRORS';
    COREWEBVIEW2_WEB_ERROR_STATUS_CERTIFICATE_REVOKED: sErr := 'CERTIFICATE_REVOKED';
    COREWEBVIEW2_WEB_ERROR_STATUS_CERTIFICATE_IS_INVALID: sErr := 'CERTIFICATE_IS_INVALID';
    COREWEBVIEW2_WEB_ERROR_STATUS_SERVER_UNREACHABLE: sErr := 'SERVER_UNREACHABLE';
    COREWEBVIEW2_WEB_ERROR_STATUS_TIMEOUT: sErr := 'TIMEOUT';
    COREWEBVIEW2_WEB_ERROR_STATUS_ERROR_HTTP_INVALID_SERVER_RESPONSE:         sErr := 'ERROR_HTTP_INVALID_SERVER_RESPONSE';
    COREWEBVIEW2_WEB_ERROR_STATUS_CONNECTION_ABORTED: sErr := 'CONNECTION_ABORTED';
    COREWEBVIEW2_WEB_ERROR_STATUS_CONNECTION_RESET: sErr := 'CONNECTION_RESET';
    COREWEBVIEW2_WEB_ERROR_STATUS_DISCONNECTED: sErr := 'DISCONNECTED';
    COREWEBVIEW2_WEB_ERROR_STATUS_CANNOT_CONNECT: sErr := 'CANNOT_CONNECT';
    COREWEBVIEW2_WEB_ERROR_STATUS_HOST_NAME_NOT_RESOLVED: sErr := 'HOST_NAME_NOT_RESOLVED';
    COREWEBVIEW2_WEB_ERROR_STATUS_OPERATION_CANCELED: sErr := 'OPERATION_CANCELED';
    COREWEBVIEW2_WEB_ERROR_STATUS_REDIRECT_FAILED: sErr := 'REDIRECT_FAILED';
    COREWEBVIEW2_WEB_ERROR_STATUS_UNEXPECTED_ERROR: sErr := 'UNEXPECTED_ERROR';
  end;
  if Length(sErr) > 0 then
    LogEntry('Navigation completed error: ' + sErr);
  FStop := Now;
  DecodeDateTime(FStop - FStart,yr,mon,day,hr,min,sec,msec);
  edNavElapsedTime.Text := IntToStr(sec);
end;

procedure TForm2.NavigationStarting(Sender: TCustomEdgeBrowser; Args: TNavigationStartingEventArgs);
var
  pwc: PWideChar;
begin
  FStart := Now;
  Args.ArgsInterface.Get_uri(pwc);
  LogEntry('Navigation Starting to ' + pwc);
end;

procedure TForm2.Button1Click(Sender: TObject);
begin
  FEB.Navigate(edit1.text);
end;

procedure TForm2.CreateWebViewCompleted(Sender: TCustomEdgeBrowser; AResult: HResult);
begin
  if AResult = 0 then
    LogEntry('Create Web View Completed Successfully')
  else
    LogEntry('Error: Failed to create Web View ' + IntToStr(AResult));
end;

end.

您可以在此处找到最新的 webview2 运行时: NuGet Gallery | Microsoft.Web.Webview2 1.0.1072.54

此外,您需要确保已安装 Microsoft Edge,最好是最新版本。

4

0 回答 0