Unless I'm missing something I think I've uncovered a bug with the TWebBrowser
in Delphi.
I have a TPageControl
with one TTabSheet
on it and in that I have a TWebBrowser
. The .SelectedEngine
is set to EdgeIfAvailable
.
I have the WebViewRuntime installed and I have the WebView2Loader.dll in my local directory. When the page has finished loading I can see the browser's .ActiveEngine
is Edge
and it works as expected.
However, when I try to create another TTabSheet
at runtime and embed a TWebBrowser
component inside that with the same .SelectedEngine
set to EdgeIfAvailable
it keeps on falling back to IE. When I look at Navigate()
in SHDocVw.pas and debug the scenario I see that .GetEdgeInterface
is nil
and thus the browser acts as IE.
I don't understand why the design time component works fine but the runtime one doesn't. Here is my design time browser DFM:
object wbMain0: TWebBrowser
Left = 0
Top = 0
Width = 1048
Height = 356
Align = alClient
TabOrder = 0
SelectedEngine = EdgeIfAvailable
OnDocumentComplete = OnDocumentComplete
ExplicitWidth = 740
ExplicitHeight = 404
ControlData = {
4C000000506C0000CB2400000000000000000000000000000000000000000000
000000004C000000000000000000000001000000E0D057007335CF11AE690800
2B2E12620A000000000000004C0000000114020000000000C000000000000046
8000000000000000000000000000000000000000000000000000000000000000
00000000000000000100000000000000000000000000000000000000}
end
When the user changes tabs I embed a new TWebBrowser
control into the TTabSheet
and navigate to the URL. Here is the TPageControl.ControlOnChange
event:
procedure TfrmMainBrowser.pcMainChange(Sender: TObject);
begin
if fbBusy then
Exit;
if pcMain.ActivePageIndex > 0 then
begin
if pcMain.Pages[pcMain.ActivePageIndex].ComponentCount = 0 then
begin
var loBrowser := TWebBrowser.Create(pcMain.Pages[pcMain.ActivePageIndex]);
loBrowser.SetParentComponent(pcMain.Pages[pcMain.ActivePageIndex]);
loBrowser.Silent := True;
loBrowser.SelectedEngine := EdgeIfAvailable;
loBrowser.Align := alClient;
loBrowser.OnDocumentComplete := OnDocumentComplete;
loBrowser.Show;
//Can't set via TWebBrowser, is this necessary?
//loBrowser.UserDataFolder := TPath.Combine(TPath.GetDirectoryName(Application.ExeName), 'CustomCache');
loBrowser.Navigate(pcMain.ActivePage.Caption);
end;
end;
end;
As a test I change the runtime creation to EdgeOnly
and that still falls back to IE. Which is odd because I expect a blank page. But instead it still uses IE.
This is from the documentation:
TWebBrowser.TSelectedEngine.EdgeOnly: the TWebBrowser attempts to use the Edge WebView2 browser control, however if this is not possible then no browsing is possible as there is no fallback option.
If I change the dynamically created TWebBrowser
component to a TEdgeBrowser
it also works correctly:
var loBrowser := TEdgeBrowser.Create(pcMain.Pages[pcMain.ActivePageIndex]);
loBrowser.SetParentComponent(pcMain.Pages[pcMain.ActivePageIndex]);
loBrowser.Align := alClient;
loBrowser.Show;
loBrowser.Navigate(pcMain.ActivePage.Caption);
What am I missing?