问题
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
回答1:
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.
回答2:
from the path of my Edge, "C:\Program Files (x86)\Microsoft\Edge\Application\msedge.exe", we may conclude the current Edge is 32bit program? wat about give a try setting platform target to x86. some guru said, with x86 set, it will work!
from the MS sample project WebView2WindowsFormsBrowser.csproj, the Microsoft.Web.WebView2 version should be "0.9.515-prerelease".
来源:https://stackoverflow.com/questions/61885845/microsoft-edge-webview2-sample-crashes-on-load