question

GuoLearn-6547 avatar image
0 Votes"
GuoLearn-6547 asked GuoLearn-6547 commented

Sometimes click something in The WebView2 Page will open a new Microsoft Edge Window,could we show it in the Webview2 replace open a new window?

With local file WebView2:
var env = await CoreWebView2Environment.CreateAsync(WebViewRuntimeFolder, DataFolder, opt));
await webView.EnsureCoreWebView2Async(env);

I want whatever you click in the WebView2,The requested showed in the WebView2 Element, not open a new Microsoft Edge Window,could you help me?

windows-wpfdotnet-wpf-xaml
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

1 Answer

HuiLiu-MSFT avatar image
0 Votes"
HuiLiu-MSFT answered GuoLearn-6547 commented

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, set e.NewWindow = (CoreWebView2)sender;
To open in another specific instance, set e.NewWindow to the other CoreWebView2 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.

· 1
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Thanks a lot.

0 Votes 0 ·