1

In my wpf application i try to show a webView2 for a login, even though when i navigate to the view and start the initialization nothing happens. I've tried many options and solutions i found in the internet and that worked for others but nothing helped.

The Code:

    private WebView2 m_webView21;
    public WebView2 WebView21 { get => m_webView21; set => SetProperty(ref m_webView21, value); }

 protected async Task<WebView2> CreateBrowserAndLoadUrlAsync(string url)
    {
      webView21 = new WebView2();
      webView21.CoreWebView2InitializationCompleted += WebView21_CoreWebView2InitializationCompleted;

      Debug.WriteLine("InitializeAsync");
      await webView21.EnsureCoreWebView2Async();
      Debug.WriteLine("WebView2 Runtime version: " + webView21.CoreWebView2.Environment.BrowserVersionString);

      SetBrowserHostVisibility(Visibility.Visible);
      if (webView21 != null && webView21.CoreWebView2 != null)
      {
        webView21.CoreWebView2.Navigate(url);
      }
      return webView21;
    }

    private void WebView21_CoreWebView2InitializationCompleted(object sender, CoreWebView2InitializationCompletedEventArgs e)
    {
      webView21.Loaded += Browser_FrameLoadEnd;
      webView21.Initialized += Browser_InitializedChanged;
    }

--Xaml Code--

  <Grid Background="Transparent">
    <wv2:WebView2 Visibility="Visible"  Source="{Binding WebView21.Source.AbsoluteUri, Mode=OneWay}"/>
  </Grid>

This is the first thing i'm calling in my viewmodel from the OnNavigatedTo(). Everything works as it should untig it comes to the EnsureCoreWebViewAsync() - from this function it never returns - just nothing happens.

When i do not initialize the webview2 and just set the source to the uri everything works fine aswell. But no events or anything get fired (e.g. NavigationCompleted, SourceChanged, etc.) and i need those events ofc.

I installed the correct runtime i also installed the WebView2 Plugin of course. Also tried different enviroments nothing helped.

So the actual question is, is it even possible to initialize the WebView2 from the Viewmodel?

KSler
  • 121
  • 9
  • Does this answer your question? [How can I initialize the WebView2 in WPF?](https://stackoverflow.com/questions/65139090/how-can-i-initialize-the-webview2-in-wpf) – Poul Bak May 17 '22 at 08:13
  • No sadly it doesn't but thanks. – KSler May 17 '22 at 08:39
  • Retry [this](https://learn.microsoft.com/en-us/microsoft-edge/webview2/get-started/wpf) from scratch and come back tell us where it failed. – Orace May 17 '22 at 10:02
  • Already went through this, Step 9.1 is where it failed. It's the same behavior as i mentioned in the question, it never returns from the await part :( – KSler May 17 '22 at 10:06
  • You're just trying to create a webview2 without placing it in parent? Do you intend to put it in a UIElement after it's initialized? – Jay Buckman May 17 '22 at 11:05
  • Yes indeed, The Webview should be shown in my View after. But i need the initialization to use the CookieManager first. I'll put the xaml code in the question aswell :) – KSler May 17 '22 at 11:09
  • I don't think you can await the initialization of a webview without it being in the gui because it's not part of the application's event loop yet. – Jay Buckman May 17 '22 at 11:28

1 Answers1

0

I found the solution. I had to use a placeholder in my xaml which later init's the webView2 in my Viewmodel.

Just change the xaml code from the question with this and it should work fine

 <ContentControl Content="{Binding WebView21, Mode=OneWay}" />

Thanks for all the helpful comments :))

Chadley08
  • 559
  • 1
  • 8
  • 26
KSler
  • 121
  • 9