【发布时间】:2020-09-05 05:03:37
【问题描述】:
我采用了最新版本的 Microsoft.Web.WebView2(0.9.515-prerelease)并添加到测试 C# WinForms 应用程序中。我使用的是 VS 2019,.NET 框架是 4.7.2。 将 WebView2 控件放在窗体上,编译并运行。应用程序在加载时崩溃,在 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 -
当我在发布模式下运行它时,得到以下异常跟踪 - System.NullReferenceException:对象引用未设置为对象的实例。
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)
如果我使用最新的稳定版 WebView2 (v 0.9.488),它会引发编译错误,因为它不反映 Microsoft.Web
请告诉我如何解决此错误。非常感谢您的帮助。 8 个月前遇到了这个问题,但我希望微软现在已经超越了这个问题。 How can I use the Microsoft Edge WebView2 control in C# windows application
Microsoft 创建的示例解决方案也有适用于 WinForms 的 C# 版本。 https://github.com/MicrosoftEdge/WebView2Samples
也尝试了 Microsoft 提供的这个示例。它也崩溃了。 https://docs.microsoft.com/en-us/microsoft-edge/webview2/gettingstarted/winforms
【问题讨论】:
-
您是否为 WebView2 安装了正确版本的 Microsoft Edge?例如 WebView2 0.9.515-prerelease 至少需要安装 Microsoft Edge 84.0.515.0? docs.microsoft.com/en-us/microsoft-edge/webview2/releasenotes
-
示例应用程序显示“确保在地址栏中输入完整的 URL。如果 URL 不以 http:// 或 https:// 开头,则会引发 ArgumentException”,并且您的应用程序是试图加载“关于:空白”。也许这就是问题所在?
-
它甚至没有到达加载事件。例外是在设计器代码本身,而设计器代码将控件添加到窗体。
-
不确定我是否遗漏了一些非常基本的东西 - 我检查了笔记本电脑中 MS Edge 的版本,它说“Microsoft Edge 是最新的。版本 81.0.416.77(官方版本)(64-少量)”。但是为什么 84.0.* 在该网络链接中声明为最低版本?
标签: visual-studio-2019 webview2