question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

BlazorWebView for WPF Documenation how access the underlying WebView2 to set additionalBrowserArguments or CreationProperties.BrowserExecutableFolder

See original GitHub issue

Description

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:closed
  • Created 2 years ago
  • Comments:9 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
Eiloncommented, Dec 29, 2021

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.).

0reactions
mkArtakMSFTcommented, Apr 1, 2022
Read more comments on GitHub >

github_iconTop Results From Across the Web

BlazorWebView for WPF Documenation how access the ...
BlazorWebView for WPF Documenation how access the underlying WebView2 to set additionalBrowserArguments or CreationProperties.BrowserExecutableFolder #661.
Read more >
WebView2 Class (Microsoft.Web.WebView2.Wpf)
You can directly access the underlying ICoreWebView2 interface and all of its functionality by accessing the CoreWebView2 property. Some of the most common...
Read more >
CoreWebView2CreationProperties Class
Its main purpose is to be set to CreationProperties in order to customize the environment and/or controller used by a WebView2 during implicit...
Read more >
Why my coreWebView2 which is object of webView2 is null?
Use the EnsureCoreWebView2Async method to initialize the underlying CoreWebView2 property. This is documented on MSDN. This property is null ...
Read more >
Taking the new Chromium WebView2 Control for a Spin in .NET
This is Part 1 of a two part article that gives an overview of the new WebView2 Web browser control that is based...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found