I am creating a object of Microsoft.Web.WebView2.WinForm.WebView2 but the sub obect of this coreWebView2 is null
Microsoft.Web.WebView2.WinForm.WebView2 webView = new Microsoft.Web.WebView2.WinForm.WebView2()
// Change some GUI properties of webView
webView.CoreWebView.NavigateUrl(url)
// I can not access the above line because CoreWebView is null
I tried everything from all over the web, but CoreWebView2
was always null
.
Turns out, I was missing the runtime for Edge.
Then I installed it using the Evergreen Standalone installer from Microsoft Edge WebView2. And then it worked!
Use the EnsureCoreWebView2Async
method to initialize the underlying CoreWebView2
property. This is documented on MSDN. This property is null
on initialization of the WebView2
class object.
CoreWebView2 The underlying CoreWebView2.
public CoreWebView2 CoreWebView2
Use this property to perform more operations on the WebView2 content than is exposed on the WebView2. This value is null until it is initialized. You can force the underlying CoreWebView2 to initialize via the InitializeAsync (EnsureCoreWebView2Async - apparently Microsoft failed to update their documentation) method.
Source (https://learn.microsoft.com/en-us/microsoft-edge/webview2/reference/winforms/0-9-515/microsoft-web-webview2-winforms-webview2)
You can use EnsureCoreWebView2Async
to make sure the object is initialized before doing any navigations. However, even easier, you can set the Source
property which make sure the initialization is triggered, if not already:
Microsoft.Web.WebView2.WinForm.WebView2 webView = new Microsoft.Web.WebView2.WinForm.WebView2();
webView.Soure = url;
As @Abbas Tambawala said in a comment, you have to make sure that your project is set for a specific bitness. The sometimes default "AnyCPU" setting will seem to work but the control won't initialize, await webView.EnsureCoreWebView2Async
will return a Task that never completes, CoreWebView2
will be null, and other symptoms will be a clue to check that setting in your project.
Also, be aware that if you are working on a solution with multiple projects, like I was... you need to make sure all the projects are consistently flagged correctly.
The DLL project I was working on was marked as X86, but the project that loaded my DLL was marked with "AnyCPU". Many moments of WTF is going on... the comment about "prefer 32 bit" to Ryan's answer gave the "Ah-HA" moment.
Also, you may want to check what event or method you are setting webView2.Source or calling EnsureCoreWebView2Async in... webView2 needs the UI thread to be completely initialized and the form shown. I generally put my webView2 init code in the Form.Shown event. Here is a link describing the Form Events order: MS-Docs Form Events
First you have to initialize webview2 using the following code.
WebView21.EnsureCoreWebView2Async()
Then you should write the following code in the event named CoreWebView2InitializationCompleted.
WebView21.CoreWebView2.Navigate(yoururl)
Use webView2.EnsureCoreWebView2Async();
at the start of program to initialize and create the event to check if it is initialized or not.
bool ensure = false;
private void webView21_CoreWebView2InitializationCompleted(object sender, Microsoft.Web.WebView2.Core.CoreWebView2InitializationCompletedEventArgs e)
{
ensure = true;
}
Working demo: https://www.youtube.com/watch?v=S6Zr5T9UXUk
I got around this issue in my visual basic project by setting a boolean variable in the WebView2.CoreWebView2Ready event then had called a one off do events loop to wait in the forms activated event to check the variable to make sure it was initialized and ready to use
I noticed even after the await Async it was still not ready to use immediately and corewebview2 remained null but this might be a vb thing
Here's an Example converted to c# from my webview2 visual basic project it should work in c#
class SurroundingClass
{
private bool FstRun = true;
private bool WebReady = false;
private void Form1_Activated(object sender, EventArgs e)
{
if (FstRun == true)
{
FstRun = false;
InitAsync();
Wait(); // wait for webview to initiaise
// code or sub here to reference the control navigate ect
}
}
private void WebView21_CoreWebView2Ready(object sender, EventArgs e)
{
WebReady = true;
}
private async void InitAsync()
{
await WebView21.EnsureCoreWebView2Async;
}
private void Wait()
{
while (!WebReady == true)
System.Windows.Forms.Application.DoEvents();
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With