Search code examples
c#winformswebview2

UI-specific WebView2 vs CoreWebView2Controller; understanding the difference and whether or not it matters


I have a WebView2 control on a Form.

I set a user data folder (UDF) and initialize like so:

CoreWebView2Environment environment = await CoreWebView2Environment.CreateAsync(userDataFolder: "<some_path>");
await webView.EnsureCoreWebView2Async(environment);
// use webView2.CoreWebView2 below

webView2 is a private field that's on my Form.

I'm exploring instead creating separate profiles in one UDF as opposed to separate UDFs as described here

Doing that looks like:

CoreWebView2Environment environment = await CoreWebView2Environment.CreateAsync(userDataFolder: "<some_path>");
CoreWebView2ControllerOptions options = environment.CreateCoreWebView2ControllerOptions();
options.ProfileName = "MyProfileName";
options.IsInPrivateModeEnabled = true;
CoreWebView2Controller controller = await environment.CreateCoreWebView2ControllerAsync(this.Handle, options);
// reference controller.CoreWebView2 below

But now, I don't need the private webView control that's on my Form and need to use CoreWebView2Controller's APIs instead of WebView2's APIs.

So I don't have a KeyDown event handler and need to use AcceleratorKeyPressed and there's apparently no Invoke(...) method on the controller that I was relying on before.

I haven't yet discovered if there is a limitation related to this API difference that matters to me, but I'm trying to understand the implications of using the Controller vs the WinForm WebView2. In people's experience does this difference matter?

(reference that came up)


Solution

  • TLDR: If you are using a UI framework for which WebView2 provides a WebView2 control (WinForms, WPF, WinUI2, WinUI3) it is recommended to use that WebView2 control class. You can write your app to use the CoreWebView2Controller directly but it requires a lot of work to hook up all aspects of hosting that the WebView2 control already performs.

    Details on the differences of WebView2 and CoreWebView2Controller: this document Main classes for WebView2 describes this in greater detail, but to summarize the parts you are asking about:

    • The CoreWebView2Controller class provides the raw access needed to directly host a CoreWebView2. It has no knowledge of a specific UI framework but instead provides direct access to all the functionality one would need to integrate it into a UI framework control or directly into your app.
    • There are also WebView2 control classes for different UI frameworks (WinForms, WPF, WinUI2, WinUI3). These wrap the CoreWebView2Controller and integrate it with their UI framework. The underlying CoreWebView2Controller is not exposed directly, but instead the WebView2 control class connects up CoreWebView2Controller behavior to the corresponding UI framework mechanism or the WebView2 control class exposes the behavior itself.

    Using CoreWebView2ControllerOptions with WebView2 controls: you can change the WebView2 profile using CoreWebView2ControllerOptions with a WebView2 control class. You would call the EnsureCoreWebView2Async method before otherwise initializing the WebView2 control. That includes before setting its Source property which also begins initialization.