I took the latest version of Microsoft.Web.WebView2 (0.9.515-prerelease) and added to a test C# WinForms application. Am using VS 2019, .NET framework is 4.7.2. Placed the WebView2 control on a form, compiled and ran. The application crashed on Load, at the below point in Form1.designer.cs.
// webView21
//
this.webView21.Location = new System.Drawing.Point(153, 66);
this.webView21.Name = "webView21";
this.webView21.Size = new System.Drawing.Size(492, 253);
this.webView21.Source = new System.Uri("about:blank", System.UriKind.Absolute);
this.webView21.TabIndex = 0;
this.webView21.Text = "webView21";
this.webView21.ZoomFactor = 1D;
//
// Form1
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(800, 450);
this.Controls.Add(this.webView21); //CRASHED HERE WITH BELOW EXCEPTION -
When I run this in Release mode, get the following exception trace - System.NullReferenceException: Object reference not set to an instance of an object.
at Microsoft.Web.WebView2.WinForms.WebView2.OnVisibleChanged(EventArgs e)
at System.Windows.Forms.Control.OnParentVisibleChanged(EventArgs e)
at System.Windows.Forms.Control.OnVisibleChanged(EventArgs e)
at System.Windows.Forms.ScrollableControl.OnVisibleChanged(EventArgs e)
at System.Windows.Forms.Form.OnVisibleChanged(EventArgs e)
at System.Windows.Forms.Control.WmShowWindow(Message& m)
at System.Windows.Forms.Control.WndProc(Message& m)
at System.Windows.Forms.ScrollableControl.WndProc(Message& m)
at System.Windows.Forms.Form.WmShowWindow(Message& m)
at System.Windows.Forms.Form.WndProc(Message& m)
at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
at System.Windows.Forms.NativeWindow.Callback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
If I take the latest stable version of WebView2 (v 0.9.488), it throws a compilation error as it does not reflect Microsoft.Web
Please let me know how to fix this error. Appreciate your help very much. Came across this question 8 months ago but am hoping Microsoft has gone beyond that now. How can I use the Microsoft Edge WebView2 control in C# windows application
The Sample solution Microsoft created does have C# version as well for WinForms. https://github.com/MicrosoftEdge/WebView2Samples
Tried this sample given by Microsoft as well. It too crashes. https://docs.microsoft.com/en-us/microsoft-edge/webview2/gettingstarted/winforms
Sometimes, your browser extensions may cause Edge to crash. In order to quickly identify the culprit, you need to disable all the browser add-ons and extensions. Then, re-enable them one by one. Once you've identified the culprit, you need to permanently delete it from your computer.
To benefit from the new or improved Office features or add-ins that rely on WebView2, no action is required on your part. We recommend that you allow WebView2 Runtime to be installed. As soon as WebView2 is available on the device, then your users can start using the Office features that rely on WebView2.
Microsoft WebView2 Runtime is a runtime application that uses Microsoft Edge to render web-based features on Microsoft 365 applications. It aims to provide users with the same experience whether they are using Office apps on the web or Windows.
Microsoft Edge WebView2 is a developer control for embedding web content in applications. It allows developers to leverage the best of what Microsoft Edge Chromium platform can offer and build seamless experiences for their users that incorporate web-based content.
you need to make sure 1) the Edge version is right. try download Canary version Edge from Url https://c2rsetup.officeapps.live.com/c2r/downloadEdge.aspx?ProductreleaseID=Edge&platform=Default&version=Edge&source=EdgeInsiderPage&Channel=Canary&language=en
and 2) set x86 as the target platform will fix the problem.
I faced the same issue but I was able to solve it. It crashes any time the traget CPU is "Any CPU" but if you change it to either "x86" or "x64", it runs successfully in my case. Seems it is not able to check and work with unspecified target CPU architecture.
I hope this saves someone lots of headaches
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