question

GGunnfs-5814 avatar image
0 Votes"
GGunnfs-5814 asked Castorix31 published

webView2 uninitalize exception on navigate

drag and drop WebView2 into a panel in winform as webView
here is my code

if (webView.CoreWebView2 == null) webviewInit();
webView.CoreWebView2.NavigateToString("https://finance.yahoo.com/quote/" + symbolTbx.Text); // this gets uninitialized exception during runtime.
...
async void webViewInit()

{
webView.Source = new Uri(" https://finance.yahoo.com/quote/");
string userDataFolder = null;
CoreWebView2EnvironmentOptions options = null;
CoreWebView2Environment env = CoreWebView2Environment.CreateAsync("", userDataFolder, options).GetAwaiter().GetResult();
await webView.EnsureCoreWebView2Async(env);
}




what should I do?
ms-edge
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

1 Answer

Castorix31 avatar image
0 Votes"
Castorix31 answered Castorix31 published

You must initialize it, either with Source as the doc says at Get started with WebView2 in WinForms apps
or
with EnsureCoreWebView2Async, like :

         private async void Form1_LoadAsync(object sender, EventArgs e)
         {
             webView21.CoreWebView2InitializationCompleted += new System.EventHandler<Microsoft.Web.WebView2.Core.CoreWebView2InitializationCompletedEventArgs>(webView21_CoreWebView2InitializationCompleted);
             await InitializeAsync();           
         }

with

         private async Task InitializeAsync()
         {
             await webView21.EnsureCoreWebView2Async(null);
         }

test URL :

         private void webView21_CoreWebView2InitializationCompleted(object sender, Microsoft.Web.WebView2.Core.CoreWebView2InitializationCompletedEventArgs e)
         {
             if (webView21 != null && webView21.CoreWebView2 != null)
             {               
                 webView21.CoreWebView2.Navigate("https://www.microsoft.com");
             }           
         }






· 4
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

so I must initialize during formload to give time to complete?
I thought the await should have taken care of that.

0 Votes 0 ·

Or just Source works for me if I don't set it with the Designer:

         public Form1()
         {
             InitializeComponent();
             webView21.Source = new System.Uri("https://www.google.com", System.UriKind.Absolute);
         }
0 Votes 0 ·

@ Castorix31
now I have
"Couldn't find a compatible Webview2 Runtime installation to host WebViews."

despite I got installed from nuget the x64 and x64 runtime

what happened

0 Votes 0 ·
Castorix31 avatar image Castorix31 GGunnfs-5814 ·

You can see this thread


0 Votes 0 ·