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?

GuoLearn 251 Reputation points
2021-10-27T02:51:14.067+00:00

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 Presentation Foundation
Windows Presentation Foundation
A part of the .NET Framework that provides a unified programming model for building line-of-business desktop applications on Windows.
2,804 questions
XAML
XAML
A language based on Extensible Markup Language (XML) that enables developers to specify a hierarchy of objects with a set of properties and logic.
824 questions
0 comments No comments
{count} votes

Accepted answer
  1. Hui Liu-MSFT 48,586 Reputation points Microsoft Vendor
    2021-10-27T03:36:40.103+00:00

    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.


0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.