BlazorWebView for WPF Documenation how access the underlying WebView2 to set additionalBrowserArguments or CreationProperties.BrowserExecutableFolder
See original GitHub issueDescription
I want to use the WebGPU feature of a preview build of edge canary with the WPF BlazorWebView.
To do this I need to set the Binaries of the webview2, which can only be done over the underlying Wpf WebView2 control. (CreationProperties.BrowserExecutableFolder) an pass some command line parameters to the control to activate WebGPU.
Now my question is, what is the best practice to access the underlying WebView2 Control?
In the sourcecode I found that it is possible to override the template https://github.com/dotnet/maui/blob/a1e46d7d78a198d6171f744c76e1e7c387da616d/src/BlazorWebView/src/Wpf/BlazorWebView.cs#L70
Now my WPF skills are not the best but I tried this code:
xmlns:blazor="clr-namespace:Microsoft.AspNetCore.Components.WebView.Wpf;assembly=Microsoft.AspNetCore.Components.WebView.Wpf"
xmlns:WpfWebView="clr-namespace:Microsoft.Web.WebView2.Wpf;assembly=Microsoft.Web.WebView2.Wpf"
<blazor:BlazorWebView Grid.Row="1" x:Name="BlazorWebView" HostPage="wwwroot/index.html" Services="{StaticResource services}">
<blazor:BlazorWebView.Template>
<ControlTemplate x:Name="WebView" TargetType="WpfWebView:WebView2">
<WpfWebView:WebView2 DefaultBackgroundColor="Green"></WpfWebView:WebView2>
</ControlTemplate>
</blazor:BlazorWebView.Template>
I get the error that TargetType can only be of Control etc, but Microsoft.Web.WebView2.Wpf.WebView2 is only a FrameworkElement.
I only managed to get it to work with the following code in the code behind: But is this the best practice? Is there a XAML way? Can there maybe a easier more intuitive way?
Can there be maybe a offical docs.microsoft
BlazorWebView.Template = new ControlTemplate()
{
// The BlazorWebView defines a control named WebView
// Could not be done in xaml since control is just a FrameworkElement
VisualTree = new FrameworkElementFactory(typeof(CustomWebView2), "WebView")
};
public class CustomWebView2 : Microsoft.Web.WebView2.Wpf.WebView2
{
public CustomWebView2()
: base()
{
this.CreationProperties.BrowserExecutableFolder = @"C:\Test\CanaryWebView\";
}
}
Also since changing the HostPage Property causes a Navigate() it seems impossible to set custom Environment settings
since calling EnsureCoreWebView2Async(customEnvSetting) is answered with the following exception: WebView2 was already initialized with a different CoreWebView2Environment. Check to see if the Source property was already set or EnsureCoreWebView2Async was previously called with different values. If I do not set HostPage after calling EnsureCoreWebView2Async() the exeption does not happen. Maybe related to this issue: https://github.com/MicrosoftEdge/WebView2Feedback/issues/1782
It seems someting causes already a initialize or a double initialize in setting HostPage
var env = CoreWebView2Environment.CreateAsync(null, null, new CoreWebView2EnvironmentOptions
{
AdditionalBrowserArguments = "--enable-features=enable-unsafe-webgpu",
}).Result;
CustomWebView2.EnsureCoreWebView2Async(env)
Thanks @Eilon 😉
Public API Changes
<blazor:BlazorWebView.Template>
<ControlTemplate x:Name="WebView" TargetType="WpfWebView:WebView2">
<WpfWebView:WebView2 DefaultBackgroundColor="Green"></WpfWebView:WebView2>
</ControlTemplate>
</blazor:BlazorWebView.Template>
Intended Use-Case
Use Edge Canary Preview Build Activate edge://flags like activating WebGPU
Issue Analytics
- State:
- Created 2 years ago
- Comments:9 (4 by maintainers)

Top Related StackOverflow Question
Yeah I think whatever event(s) we would expose would give you access to the objects so that you can do any extra custom work you need to. For example, let’s say right after the WebView2 is created, or when CoreWebView2 becomes available, we could raise an event so that the handler could do whatever they want with it (set properties, hook events, etc.).
Closing this as a dupe of https://github.com/dotnet/maui/issues/5512