I have a browser class in which I have added webview2 Control.
I have use case where I delete the webview2 browser in my class (check ShowData
method).
If required again I create a new instance of webview2 in my class (check InitializeWebBrowser
) and navigate to the page.
This is working fine in the installed application. Loading correctly without any issues.
From application built thru code in local dev first-time webview2 browser is shown. EnsureCoreWebView2Async
gets called through constructor and CoreWebView2InitializationCompleted
event is triggered and page is getting loaded. After deleting the webview2 instance and creating a new one EnsureCoreWebView2Async
is called but CoreWebView2InitializationCompleted
is not called at all, even if we wait for a long time.
Note: CoreWebView2InitializationCompleted
event triggered when webview2.dispose is called with failure status.
Any idea why CoreWebView2InitializationCompleted
is not called second time?
public MyBrowser()
{
InitializeComponent();
InitializeBrowserCoreWebViewAsync();
}
public async void InitializeBrowserCoreWebViewAsync()
{
webBrowser.CoreWebView2InitializationCompleted += WebBrowser_CoreWebView2InitializationCompleted;
await webBrowser.EnsureCoreWebView2Async(null);
}
private void WebBrowser_CoreWebView2InitializationCompleted(object sender, CoreWebView2InitializationCompletedEventArgs e)
{
if (e.IsSuccess)
{
if (!string.IsNullOrEmpty(m_sDocumentText)) // m_sDocumentText is Private which contains HTML Content
{
webBrowser.NavigateToString(m_sDocumentText);
m_sDocumentText = string.Empty;
}
}
else
{
Log.Error($"WebBrowser_CoreWebView2InitializationCompleted - CoreWebView2 Initialization failed", e.InitializationException);
}
}
public void ShowData(string url)
{
if (!String.IsNullOrEmpty(url))
{
Uri uri = new Uri(url);
InitializeWebBrowser();
webBrowser.Source = uri;
}
else
{
if (webBrowser != null)
{
webBrowser.Dispose();
webBrowser = null;
}
}
}
private void InitializeWebBrowser()
{
if (webBrowser == null)
{
webBrowser = new Microsoft.Web.WebView2.WinForms.WebView2();
webBrowser.Dock = DockStyle.Fill;
webBrowser.Location = new System.Drawing.Point(0, 0);
webBrowser.MinimumSize = new System.Drawing.Size(20, 20);
webBrowser.Name = "webBrowser";
webBrowser.Size = new System.Drawing.Size(150, 150);
webBrowser.TabIndex = 0;
Controls.Clear();
Controls.Add(this.webBrowser);
InitializeBrowserCoreWebViewAsync();
}
}
public void SetDocumentText(string docText)
{
InitializeWebBrowser();
if (webBrowser.CoreWebView2 != null)
webBrowser.NavigateToString(docText);
else
m_sDocumentText = docText;
}
Addition Info :
Basically, WebView2 Control itself is not visible after disposing and creating new instance
I have subscribed to Webview2 VisibleChanged
event. After calling EnsureCoreWebView2Async
, VisibleChanged
event is triggered with webview2.visible
property having value false. (second time)
After Initialization, Explicitly called the below method with Visible Property set to true
public void ShowWebBrowser()
{
if (webBrowser?.Visible == false)
{
webBrowser.Visible = true;
}
}
But VisibleChanged Event gets triggered with webview2.visible property value false even though it was set to true
This is a kinda strange issue it used to happen only on some machines not all.
After making the below change it started to work for me.
The main changes are stopping the Initialization of corewebview2 in the constructor and doing it in the normal method.
Previously changes were like first-time corewebview2 Initialization was done in the constructor and the second time onwards it was happening in the Normal method after creating a new instance.
Change 1: Stopped initialization of CoreWebView2 in Constructor
public MyBrowser()
{
InitializeComponent();
//InitializeBrowserCoreWebViewAsync(); --> Removed this line
}
Change 2: In the below method previously was calling InitializeBrowserCoreWebViewAsync() only when creating new instance now moved outside to call everytime.
private void InitializeWebBrowser()
{
if (webBrowser == null)
{
webBrowser = new Microsoft.Web.WebView2.WinForms.WebView2();
webBrowser.Dock = DockStyle.Fill;
webBrowser.Location = new System.Drawing.Point(0, 0);
webBrowser.MinimumSize = new System.Drawing.Size(20, 20);
webBrowser.Name = "webBrowser";
webBrowser.Size = new System.Drawing.Size(150, 150);
webBrowser.TabIndex = 0;
Controls.Clear();
Controls.Add(this.webBrowser);
// InitializeBrowserCoreWebViewAsync(); --> Moved outside if condition
}
InitializeBrowserCoreWebViewAsync();
}
Rest Remained Same:
public async void InitializeBrowserCoreWebViewAsync()
{
if (webBrowser.CoreWebView2 == null)
{
webBrowser.CoreWebView2InitializationCompleted += WebBrowser_CoreWebView2InitializationCompleted;
await webBrowser.EnsureCoreWebView2Async(null);
}
}
private void WebBrowser_CoreWebView2InitializationCompleted(object sender, CoreWebView2InitializationCompletedEventArgs e)
{
if (e.IsSuccess)
{
if (!string.IsNullOrEmpty(m_sDocumentText)) // m_sDocumentText is Private which contains HTML Content
{
webBrowser.NavigateToString(m_sDocumentText);
m_sDocumentText = string.Empty;
}
}
else
{
Log.Error($"WebBrowser_CoreWebView2InitializationCompleted - CoreWebView2 Initialization failed", e.InitializationException);
}
}
public void ShowData(string url)
{
if (!String.IsNullOrEmpty(url))
{
Uri uri = new Uri(url);
InitializeWebBrowser();
webBrowser.Source = uri;
}
else
{
if (webBrowser != null)
{
webBrowser.Dispose();
webBrowser = null;
}
}
}
public void SetDocumentText(string docText)
{
InitializeWebBrowser();
if (webBrowser.CoreWebView2 != null)
webBrowser.NavigateToString(docText);
else
m_sDocumentText = docText;
}