Wac*_*kie 5 .net c# winforms webview2
我正在将浏览器集成到我的软件中。当我继续打开新的浏览器选项卡时它正在工作,但是一旦我关闭一个选项卡并尝试打开新的选项卡,它就会在以下代码中产生异常。
public async Task InitCore()
{
try
{
// Initialization.
await webView.EnsureCoreWebView2Async(null);
// This line gives exception if I close a tab and reopen as it gives exception in Initialization.
}
catch (Exception ex)
{
Enumerations.WriteToLog(Enumerations.LogType.Misc, "Browser.InitCore " + ex.ToString());
}
}
// Subscribing events.
private void AfterCoreReady(object sender,EventArgs e)
{
label1.Visible = false;
this.webView.CoreWebView2.ContentLoading += webView_ContentLoading;
this.webView.CoreWebView2.NewWindowRequested += webView_NewWindowRequested;
}
Run Code Online (Sandbox Code Playgroud)
关闭选项卡后重新初始化时出现以下异常:
CustomWebView2.OnEnter System.Runtime.InteropServices.COMException (0x8007139F):
The group or resource is not in the correct state to perform the requested operation. (Exception from HRESULT: 0x8007139F)
at System.Runtime.InteropServices.Marshal.ThrowExceptionForHRInternal(Int32 errorCode, IntPtr errorInfo)
at System.Runtime.InteropServices.Marshal.ThrowExceptionForHR(Int32 errorCode)
at Microsoft.Web.WebView2.Core.CoreWebView2Environment.<CreateCoreWebView2ControllerAsync>d__17.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at Microsoft.Web.WebView2.WinForms.WebView2.<InitCoreWebView2Async>d__4.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.GetResult()
at ProChart.Controls.Browser.<InitCore>d__16.MoveNext() in
Browser.cs:line 98
Run Code Online (Sandbox Code Playgroud)
这就像每次导航到新页面时都会调用“Init”方法。
如果没有看到您的完整代码,我无法确定,但 WebView2 控件只能初始化一次。
一般来说,最好的方法是拨打您的等待电话
await webView.EnsureCoreWebView2Async(null);
Run Code Online (Sandbox Code Playgroud)
在主应用程序中启动,例如 Windows 窗体或 WPF 应用程序中主窗体的构造函数。
然而,在较新的版本中,您不需要等待您一直在使用的呼叫。
只需将初始化代码添加到构造函数中,如下所示:
public FrmMainForm()
{
InitializeComponent();
webview.Height = 720;
// Webview initialisation handler, called when control instantiated and ready
webview.CoreWebView2InitializationCompleted += Webview_CoreWebView2InitializationCompleted;
}
Run Code Online (Sandbox Code Playgroud)
在上面的示例中,我将控件嵌入到 Windows 窗体桌面应用程序中,因此我将其放在主窗体构造函数中。
一旦 webview2 控件准备好使用,“CoreWebView2InitializationCompleted”事件将被触发,然后您可以在该事件处理程序中初始化 webview 中的内容,例如 url 拦截、javascript 注入和 C# 类注入。
private void Webview_CoreWebView2InitializationCompleted(object sender, CoreWebView2InitializationCompletedEventArgs e)
{
// Custom URL handler (All URLS starting "http://app/" are intercepted directly by the application
webview.CoreWebView2.AddWebResourceRequestedFilter("http://app/*", CoreWebView2WebResourceContext.All);
webview.CoreWebView2.WebResourceRequested += WebResourceRequested;
// Load in our custom JS API files
webview.CoreWebView2.AddScriptToExecuteOnDocumentCreatedAsync(JsLoader.LoadApi("BrowserOverrides.js"));
// Show dev tools by default
webview.CoreWebView2.OpenDevToolsWindow();
// Other misc settings
webview.CoreWebView2.Settings.UserAgent = DEFAULTUA;
}
Run Code Online (Sandbox Code Playgroud)
我的 github 上有一些演示代码,其中包含有关如何使用所有这些功能的更多示例:
https://github.com/shawty/hbbtvbrowser实验
但请注意,此代码使用旧版本的 webview2,我的代码中的某些操作在新版本中略有不同。