You could use CoreWebView2.NewWindowRequested to set whether to open a new window.
To completely suppress the popup, set
e.Handled = true;
To show the popup content in the same window, sete.NewWindow = (CoreWebView2)sender;
To open in another specific instance, sete.NewWindow
to the otherCoreWebView2
instance.
For example:
The code of xaml:
<Window x:Class="WpfApp1.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:WpfApp1"
mc:Ignorable="d"
xmlns:wv2="clr-namespace:Microsoft.Web.WebView2.Wpf;assembly=Microsoft.Web.WebView2.Wpf"
Title="MainWindow" Height="450" Width="800" Loaded="Window_Loaded">
<DockPanel>
<DockPanel DockPanel.Dock="Top">
<Button x:Name="ButtonGo" DockPanel.Dock="Right" Click="ButtonGo_Click" Content="Go"/>
<TextBox Name = "addressBar"/>
</DockPanel>
<wv2:WebView2 Name = "webView" Source = "https://www.bing.com/" />
</DockPanel>
</Window>
The code of xaml.cs:
using Microsoft.Web.WebView2.Core;
using System.Windows;
namespace WpfApp1
{
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
private void ButtonGo_Click(object sender, RoutedEventArgs e)
{
if (webView != null && webView.CoreWebView2 != null)
{
webView.CoreWebView2.Navigate(addressBar.Text);
}
}
private void webView_NewWindowRequested(object sender, CoreWebView2NewWindowRequestedEventArgs e)
{
e.NewWindow = (CoreWebView2)sender;
//e.Handled = true;
}
private async void Window_Loaded(object sender, RoutedEventArgs e)
{
await webView.EnsureCoreWebView2Async();
webView.CoreWebView2.NewWindowRequested += webView_NewWindowRequested;
}
}
}
If the answer is the right solution, please click Accept Answer and kindly upvote it. If you have extra questions about this answer, please click Comment.
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.