Home > Enterprise >  WebView2 not initializing
WebView2 not initializing

Time:10-16

I created a simple WPF client app in .net6.0 and incorporated WebView2 control. I deployed/copied this app to a different machine and also installed .NET6.0 runtime there. Upon running this app, I only see the main window and NO WebView2 initialized/created. Note this the same implementation is working on my dev machine.

public MainWindow()
{
    InitializeComponent();
    InitializeWebView2();
}

private async Task InitializeWebView2()
{
    //myPanel.Children.Add(webView2);
    //await webView2.EnsureCoreWebView2Async();
    ((Action)(async () =>
    {
        try
        {

            CoreWebView2Environment env = await CoreWebView2Environment.CreateAsync(null, "another_dir");
            WebView2 webview = new WebView2();

            /*
            webview.Source = new Uri("https://www.bing.com");
            */

            var result = webview.EnsureCoreWebView2Async(env).GetAwaiter();
            result.OnCompleted(() =>
            {
                try
                {
                    result.GetResult();
                }
                catch (Exception e)
                {
                    Console.WriteLine(e);
                }
            });


            myPanel.Children.Add(webview);
            webview.NavigateToString("https://google.com");
        }
        catch (Exception e)
        {
            Console.WriteLine(e);
        }
    })).Invoke();
}

CodePudding user response:

EnsureCoreWebView2Async is supposed to be awaited before you set the Source:

private async Task InitializeWebView2()
{
    var webview = new WebView2();
    myPanel.Children.Add(webview);

    var env = await CoreWebView2Environment.CreateAsync(null, "another_dir");
    var result = await webview.EnsureCoreWebView2Async(env);
    webview.Source = new Uri("https://www.bing.com");
}
  • Related