Closed HocNie closed 3 years ago
Hi @HocNie What error code are you getting and/or which line of code is throwing the error? I've been learning the WebView2 control over the last week and have probably dealt with the issue you are having...
Hi,Just The Code: e.NewWindow = newWebView.CoreWebView2 Not Work. Private Shared Async Sub OnNewWindowRequested(sender As Object, e As CoreWebView2NewWindowRequestedEventArgs) e.Handled = True Dim deferral = e.GetDeferral Dim newTabPage As New EdgeTabControl.EdgeTabPage With {.Loading = True} Dim newWebView = Await NewWebPage(newTabPage) DHL365_Edge.edgeTabControl.Add(newTabPage, True) 'Runtime 92.0.902.62 '----------------------------------------------------------------------------------------------------- newWebView.CoreWebView2.Navigate(e.Uri) 'Work 'e.NewWindow = newWebView.CoreWebView2 'Not Work '----------------------------------------------------------------------------------------------------- deferral.Complete() End Sub
Hi @HocNie What error code are you getting and/or which line of code is throwing the error? I've been learning the WebView2 control over the last week and have probably dealt with the issue you are having...
Hi,Just The Code: e.NewWindow = newWebView.CoreWebView2 Not Work ,TKS. Private Shared Async Sub OnNewWindowRequested(sender As Object, e As CoreWebView2NewWindowRequestedEventArgs) e.Handled = True Dim deferral = e.GetDeferral Dim newTabPage As New EdgeTabControl.EdgeTabPage With {.Loading = True} Dim newWebView = Await NewWebPage(newTabPage) DHL365_Edge.edgeTabControl.Add(newTabPage, True) 'Runtime 92.0.902.62 '----------------------------------------------------------------------------------------------------- newWebView.CoreWebView2.Navigate(e.Uri) 'Work 'e.NewWindow = newWebView.CoreWebView2 'Not Work '----------------------------------------------------------------------------------------------------- deferral.Complete() End Sub
@HocNie Can you please post the exception details as well.
Hi,Dear Foreign friends, there is no error in the code, but I want to display it in the tab page instead of popping up a new browser window. Use code :e.NewWindow = newWebView.CoreWebView2 ,And Not Use Code :ewWebView.CoreWebView2.Navigate(e.Uri)。。。Tks.
Ah, ok, I understand. I had this problem last week. I had a project based on SharpBrowser using CEFSharp which I wanted to convert to use WebView2. (Now that I’ve done the conversion, my preference is to continue to use WebView2.)
Here’s a cut down version of my working solution in C# and converted to VB.NET. It has all the necessary functions to achieve what you are wanting to do, however there is not enough here for you to run it through the debugger. If I have time later in the week, I may be able to create a sample solution which I can upload.
using System;
using System.Threading.Tasks;
using Microsoft.Web.WebView2.Core;
using Microsoft.Web.WebView2.WinForms;
namespace DemoApp.UIComponentsBase.Webbrowsers
{
class UserControlFullWebbrowser()
{
void CreateInitialBrowser()
{
WebView2Fixed browser;
if (uri == null)
{
browser = new WebView2Fixed();
}
else
{
browser = new WebView2Fixed(uri, existingEnvironment);
}
browser.Dock = DockStyle.Fill;
tabStrip.Controls.Add(browser);
browser.CoreWebView2InitializationCompleted += OnCoreWebView2Initialized;
}
void OnCoreWebView2Initialized(object sender, CoreWebView2InitializationCompletedEventArgs e)
{
((WebView2)sender).CoreWebView2.NewWindowRequested += OpenUrlInNewTab;
}
internal void OpenUrlInNewTab(object sender, CoreWebView2NewWindowRequestedEventArgs e)
{
var newBrowser = AddNewBrowserTab(new Uri(e.Uri), ((CoreWebView2)sender).Environment);
newBrowser.Deferral = e.GetDeferral();
newBrowser.EventArgs = e;
}
WebView2Tab AddNewBrowser(CustomTabPage tabStrip, Uri uri = null)
{
WebView2Fixed browser;
browser = uri == null ? new WebView2Fixed() : new WebView2Fixed(uri);
browser.Dock = DockStyle.Fill;
tabStrip.Controls.Add(browser);
browser.CoreWebView2InitializationCompleted += OnCoreWebView2Initialized;
// new tab obj
var tab = new WebView2Tab
{
Browser = browser,
Tab = tabStrip,
Uri = uri,
};
// save tab obj in tabstrip
tabStrip.Tag = tab;
return tab;
}
}
class WebView2Fixed : WebView2
{
internal CoreWebView2Deferral Deferral;
internal CoreWebView2NewWindowRequestedEventArgs EventArgs;
public WebView2Fixed()
{
Initialize();
}
public WebView2Fixed(Uri source)
{
Initialize(existingEnvironment);
Source = source;
CoreWebView2InitializationCompleted += WVBrowser_CoreWebView2InitializationCompleted;
}
async void Initialize()
{
CreationProperties = new CoreWebView2CreationProperties
{
BrowserExecutableFolder = @"Microsoft.WebView2.FixedVersionRuntime.92.0.902.78.x86",
Language = "en-GB"
};
await EnsureCoreWebView2Async(existingEnvironment);
while (CoreWebView2 == null)
{
await Task.Yield();
}
}
void WVBrowser_CoreWebView2InitializationCompleted(object sender, CoreWebView2InitializationCompletedEventArgs e)
{
if (e.IsSuccess)
{
// Was deferral set in previous instance?
if (Deferral != null)
{ //https://github.com/MicrosoftEdge/WebView2Feedback/issues/423
// Set this instance, complete NewWindowRequested and clear deferral
EventArgs.NewWindow = CoreWebView2;
EventArgs.Handled = true;
Deferral.Complete();
Deferral = null;
EventArgs = null;
}
}
}
}
}
Imports System
Imports System.Threading.Tasks
Imports Microsoft.Web.WebView2.Core
Imports Microsoft.Web.WebView2.WinForms
Namespace DemoApp.UIComponentsBase.Webbrowsers
Class UserControlFullWebbrowser
Private Sub CreateInitialBrowser()
Dim browser As WebView2Fixed
If (uri Is Nothing) Then
browser = New WebView2Fixed
Else
browser = New WebView2Fixed(uri, existingEnvironment)
End If
browser.Dock = DockStyle.Fill
tabStrip.Controls.Add(browser)
browser.CoreWebView2InitializationCompleted = (browser.CoreWebView2InitializationCompleted + OnCoreWebView2Initialized)
End Sub
Private Sub OnCoreWebView2Initialized(ByVal sender As Object, ByVal e As CoreWebView2InitializationCompletedEventArgs)
CType(sender,WebView2).CoreWebView2.NewWindowRequested = (CType(sender,WebView2).CoreWebView2.NewWindowRequested + OpenUrlInNewTab)
End Sub
Friend Sub OpenUrlInNewTab(ByVal sender As Object, ByVal e As CoreWebView2NewWindowRequestedEventArgs)
Dim newBrowser = AddNewBrowserTab(New Uri(e.Uri), CType(sender,CoreWebView2).Environment)
newBrowser.Deferral = e.GetDeferral
newBrowser.EventArgs = e
End Sub
Private Function AddNewBrowser(ByVal tabStrip As CustomTabPage, Optional ByVal uri As Uri = Nothing) As WebView2Tab
Dim browser As WebView2Fixed
If (uri Is Nothing) Then
browser = New WebView2Fixed
Else
browser = New WebView2Fixed(uri, existingEnvironment)
End If
browser.Dock = DockStyle.Fill
tabStrip.Controls.Add(browser)
browser.CoreWebView2InitializationCompleted = (browser.CoreWebView2InitializationCompleted + OnCoreWebView2Initialized)
Dim tab = New WebView2Tab() {Browser=browser, Tab=tabStrip, Uri=uri}
tabStrip.Tag = tab
Return tab
End Function
End Class
Class WebView2Fixed
Inherits WebView2
Friend Deferral As CoreWebView2Deferral
Friend EventArgs As CoreWebView2NewWindowRequestedEventArgs
Public Sub New()
MyBase.New
Me.Initialize
End Sub
Public Sub New(ByVal source As Uri)
MyBase.New
Me.Initialize(existingEnvironment)
Source = source
CoreWebView2InitializationCompleted = (CoreWebView2InitializationCompleted + WVBrowser_CoreWebView2InitializationCompleted)
End Sub
Private Async Sub Initialize()
CreationProperties = New CoreWebView2CreationProperties() {BrowserExecutableFolder=Microsoft.WebView2.FixedVersionRuntime.92.0.902.78.x86, Language=en-GB}
EnsureCoreWebView2Async(existingEnvironment)
While (CoreWebView2 Is Nothing)
Task.Yield
End While
End Sub
Private Sub WVBrowser_CoreWebView2InitializationCompleted(ByVal sender As Object, ByVal e As CoreWebView2InitializationCompletedEventArgs)
If e.IsSuccess Then
' Was deferral set in previous instance?
If (Not (Me.Deferral) Is Nothing) Then
'https://github.com/MicrosoftEdge/WebView2Feedback/issues/423
' Set this instance, complete NewWindowRequested and clear deferral
Me.EventArgs.NewWindow = CoreWebView2
Me.EventArgs.Handled = true
Me.Deferral.Complete
Me.Deferral = Nothing
Me.EventArgs = Nothing
End If
End If
End Sub
End Class
End Namespace
Ah, ok, I understand. I had this problem last week. I had a project based on SharpBrowser using CEFSharp which I wanted to convert to use WebView2. (Now that I’ve done the conversion, my preference is to continue to use WebView2.)
Here’s a cut down version of my working solution in C# and converted to VB.NET. It has all the necessary functions to achieve what you are wanting to do, however there is not enough here for you to run it through the debugger. If I have time later in the week, I may be able to create a sample solution which I can upload.
using System; using System.Threading.Tasks; using Microsoft.Web.WebView2.Core; using Microsoft.Web.WebView2.WinForms; namespace DemoApp.UIComponentsBase.Webbrowsers { class UserControlFullWebbrowser() { void CreateInitialBrowser() { WebView2Fixed browser; if (uri == null) { browser = new WebView2Fixed(); } else { browser = new WebView2Fixed(uri, existingEnvironment); } browser.Dock = DockStyle.Fill; tabStrip.Controls.Add(browser); browser.CoreWebView2InitializationCompleted += OnCoreWebView2Initialized; } void OnCoreWebView2Initialized(object sender, CoreWebView2InitializationCompletedEventArgs e) { ((WebView2)sender).CoreWebView2.NewWindowRequested += OpenUrlInNewTab; } internal void OpenUrlInNewTab(object sender, CoreWebView2NewWindowRequestedEventArgs e) { var newBrowser = AddNewBrowserTab(new Uri(e.Uri), ((CoreWebView2)sender).Environment); newBrowser.Deferral = e.GetDeferral(); newBrowser.EventArgs = e; } WebView2Tab AddNewBrowser(CustomTabPage tabStrip, Uri uri = null) { WebView2Fixed browser; browser = uri == null ? new WebView2Fixed() : new WebView2Fixed(uri); browser.Dock = DockStyle.Fill; tabStrip.Controls.Add(browser); browser.CoreWebView2InitializationCompleted += OnCoreWebView2Initialized; // new tab obj var tab = new WebView2Tab { Browser = browser, Tab = tabStrip, Uri = uri, }; // save tab obj in tabstrip tabStrip.Tag = tab; return tab; } } class WebView2Fixed : WebView2 { internal CoreWebView2Deferral Deferral; internal CoreWebView2NewWindowRequestedEventArgs EventArgs; public WebView2Fixed() { Initialize(); } public WebView2Fixed(Uri source) { Initialize(existingEnvironment); Source = source; CoreWebView2InitializationCompleted += WVBrowser_CoreWebView2InitializationCompleted; } async void Initialize() { CreationProperties = new CoreWebView2CreationProperties { BrowserExecutableFolder = @"Microsoft.WebView2.FixedVersionRuntime.92.0.902.78.x86", Language = "en-GB" }; await EnsureCoreWebView2Async(existingEnvironment); while (CoreWebView2 == null) { await Task.Yield(); } } void WVBrowser_CoreWebView2InitializationCompleted(object sender, CoreWebView2InitializationCompletedEventArgs e) { if (e.IsSuccess) { // Was deferral set in previous instance? if (Deferral != null) { //https://github.com/MicrosoftEdge/WebView2Feedback/issues/423 // Set this instance, complete NewWindowRequested and clear deferral EventArgs.NewWindow = CoreWebView2; EventArgs.Handled = true; Deferral.Complete(); Deferral = null; EventArgs = null; } } } } }
Imports System Imports System.Threading.Tasks Imports Microsoft.Web.WebView2.Core Imports Microsoft.Web.WebView2.WinForms Namespace DemoApp.UIComponentsBase.Webbrowsers Class UserControlFullWebbrowser Private Sub CreateInitialBrowser() Dim browser As WebView2Fixed If (uri Is Nothing) Then browser = New WebView2Fixed Else browser = New WebView2Fixed(uri, existingEnvironment) End If browser.Dock = DockStyle.Fill tabStrip.Controls.Add(browser) browser.CoreWebView2InitializationCompleted = (browser.CoreWebView2InitializationCompleted + OnCoreWebView2Initialized) End Sub Private Sub OnCoreWebView2Initialized(ByVal sender As Object, ByVal e As CoreWebView2InitializationCompletedEventArgs) CType(sender,WebView2).CoreWebView2.NewWindowRequested = (CType(sender,WebView2).CoreWebView2.NewWindowRequested + OpenUrlInNewTab) End Sub Friend Sub OpenUrlInNewTab(ByVal sender As Object, ByVal e As CoreWebView2NewWindowRequestedEventArgs) Dim newBrowser = AddNewBrowserTab(New Uri(e.Uri), CType(sender,CoreWebView2).Environment) newBrowser.Deferral = e.GetDeferral newBrowser.EventArgs = e End Sub Private Function AddNewBrowser(ByVal tabStrip As CustomTabPage, Optional ByVal uri As Uri = Nothing) As WebView2Tab Dim browser As WebView2Fixed If (uri Is Nothing) Then browser = New WebView2Fixed Else browser = New WebView2Fixed(uri, existingEnvironment) End If browser.Dock = DockStyle.Fill tabStrip.Controls.Add(browser) browser.CoreWebView2InitializationCompleted = (browser.CoreWebView2InitializationCompleted + OnCoreWebView2Initialized) Dim tab = New WebView2Tab() {Browser=browser, Tab=tabStrip, Uri=uri} tabStrip.Tag = tab Return tab End Function End Class Class WebView2Fixed Inherits WebView2 Friend Deferral As CoreWebView2Deferral Friend EventArgs As CoreWebView2NewWindowRequestedEventArgs Public Sub New() MyBase.New Me.Initialize End Sub Public Sub New(ByVal source As Uri) MyBase.New Me.Initialize(existingEnvironment) Source = source CoreWebView2InitializationCompleted = (CoreWebView2InitializationCompleted + WVBrowser_CoreWebView2InitializationCompleted) End Sub Private Async Sub Initialize() CreationProperties = New CoreWebView2CreationProperties() {BrowserExecutableFolder=Microsoft.WebView2.FixedVersionRuntime.92.0.902.78.x86, Language=en-GB} EnsureCoreWebView2Async(existingEnvironment) While (CoreWebView2 Is Nothing) Task.Yield End While End Sub Private Sub WVBrowser_CoreWebView2InitializationCompleted(ByVal sender As Object, ByVal e As CoreWebView2InitializationCompletedEventArgs) If e.IsSuccess Then ' Was deferral set in previous instance? If (Not (Me.Deferral) Is Nothing) Then 'https://github.com/MicrosoftEdge/WebView2Feedback/issues/423 ' Set this instance, complete NewWindowRequested and clear deferral Me.EventArgs.NewWindow = CoreWebView2 Me.EventArgs.Handled = true Me.Deferral.Complete Me.Deferral = Nothing Me.EventArgs = Nothing End If End If End Sub End Class End Namespace
Thank you very much for solving my big problem.TKS.
Thanks for the question @HocNie, and big thanks to @JaseRandall for the solution!
Code In Form:
Code In EdgeWraper,And Some Code Not Work
`Imports Microsoft.Web.WebView2.Core Imports Microsoft.Web.WebView2.WinForms Public Class EdgeWraper Private Shared coreWebView2Environment As CoreWebView2Environment
End Class `