winforms e.NewWindow =(CoreWebView2)发送方仍会产生单独的示例

tuwxkamq  于 2022-11-17  发布在  其他
关注(0)|答案(2)|浏览(172)

我想导航到URL,而不是打开一个单独的示例。
无论我做什么,它仍然打开WebView2的另一个示例。

private void CoreWebView2_NewWindowRequested(object sender,
            CoreWebView2NewWindowRequestedEventArgs e)
        {
            //e.NewWindow = webView21.CoreWebView2;
            e.NewWindow = (CoreWebView2)sender;
            //e.Handled = true;
        }

here's the original post,我需要做些什么才能让它处理新窗口请求?

nwnhqdif

nwnhqdif1#

要在请求新的弹出窗口时收到通知,请订阅CoreWebView2的NewWindowRequested
当有产生快显的要求时,会引发事件。按一下只会让浏览器巡览至不同URI的链接,并不会引发事件(没有快显)。
如果用户点击浏览器提供的标准上下文菜单的Open link in new window(sic)选项,则可以请求新的弹出窗口。
或者如果网页在没有用户干预的情况下生成一个。
不幸的是,e.IsUserInitiated属性始终为true,因此您可能很难确定(在不插入JavaScript的情况下)是否应该阻止弹出窗口(如果您希望这样做的话)。
当请求新的弹出窗口时,您可以通过设置**e.Handled = true**不加选择地阻止它。
如果要打开同一窗口的新Window URI,可以指定以下任一项:

e.Handled = true;
e.NewWindow = (CoreWebView2)sender;
// or  
e.Handled = true;
((CoreWebView2)sender).Navigate(e.Uri);

示例WebView2主处理程序表单:

using Microsoft.Web.WebView2.Core;

public partial class MainForm : Form
{
    public MainForm() => InitializeComponent();

    protected override async void OnLoad(EventArgs e)
    {
        base.OnLoad(e);
        webView2.CoreWebView2InitializationCompleted += OnCoreWebView2InitializationCompleted;
        var env = await CoreWebView2Environment.CreateAsync(null, null, null);
        await webView2.EnsureCoreWebView2Async(env);
        webView2.Source = new Uri("https://www.somesite.com");
    }

    private void OnCoreWebView2InitializationCompleted(object sender, CoreWebView2InitializationCompletedEventArgs e)
    {
        webView2.CoreWebView2.NewWindowRequested += OnNewWindowRequested;
    }

    private void OnNewWindowRequested(object sender, CoreWebView2NewWindowRequestedEventArgs e)
    {
        // Open the Uri requested in the current Window
        e.Handled = true;
        ((CoreWebView2)sender).Navigate(e.Uri);
    }
  
    // Or, if you want to handle Popup Windows using your own Form template
    // => Note that it's the same event handler as above, pick one, not both!
    private void OnNewWindowRequested(object sender, CoreWebView2NewWindowRequestedEventArgs e)
    {
        // Open the Uri requested in a new instance of the PopupWindow Form
        var deferral = e.GetDeferral();
        e.Handled = true;
        var popup = new PopupWindow(deferral, e);
        popup.Show();
    }
}

如果要创建一个显示弹出窗口的新窗体,则需要一个窗体模板(可以只是一个包含WebView2控件的窗体),该模板接收由e.GetDeferral()返回的CoreWebView2Deferral
在该表单的初始化过程中,检查CoreWebView2Deferral对象是否为空,如果不是,则调用其Complete()方法完成延迟事件。
然后订阅NewWindowRequested事件,以便在请求新的弹出窗口时执行相同的操作(除非您想阻止它)。
当然,您可以在Tabbed Control中显示这些表单,以生成标准的选项卡式视图,这在所有WebBrowser中都很常见。
样本**PopupWindow表格**:

using Microsoft.Web.WebView2.Core;
using System.Windows.Forms;

public partial class PopupWindow : Form
{
    public PopupWindow() => InitializeComponent();
    public PopupWindow(CoreWebView2Deferral deferral, CoreWebView2NewWindowRequestedEventArgs args) 
        : this() {
        Core2Deferral = deferral;
        NewWindowArgs = args;
    }

    protected virtual CoreWebView2Deferral Core2Deferral { get; private set; }
    protected virtual CoreWebView2NewWindowRequestedEventArgs NewWindowArgs { get; private set; }

    protected async override void OnLoad(EventArgs e)
    {
        base.OnLoad(e);
        webView2.CoreWebView2InitializationCompleted += OnCoreWebView2InitializationCompleted;

        var env = await CoreWebView2Environment.CreateAsync(null, null, null);
        await webView2.EnsureCoreWebView2Async(env);
    }

    private void OnCoreWebView2InitializationCompleted(object sender, CoreWebView2InitializationCompletedEventArgs e)
    {
        webView2.CoreWebView2.Settings.AreDefaultContextMenusEnabled = true;
        if (Core2Deferral != null) {
            NewWindowArgs.NewWindow = webView2.CoreWebView2;
            Core2Deferral.Complete();
        }
        webView2.CoreWebView2.NewWindowRequested += OnNewWindowRequested;
    }

    private void OnNewWindowRequested(object sender, CoreWebView2NewWindowRequestedEventArgs e)
    {
        e.Handled = true;
        var popup = new PopupWindow(e.GetDeferral(), e);
        popup.Show();
    }
}
omvjsjqw

omvjsjqw2#

如何让它像真实的的网页浏览器一样工作。简单地,在一个新的标签页中打开。)
首先,创建一个继承自WebView 2的新类,并在其中包含一个TabControl字段:

internal class WebViewInTab:WebView2
{
    TabControl tabCtrl;

    public WebViewInTab(TabControl tabCtrl) :base()
        {
            Dock = DockStyle.Fill; // necessary for showing 
            this.tabCtrl = tabCtrl; // for adding new TabPage controls
            CoreWebView2InitializationCompleted += WebViewInTab_CoreWebView2InitializationCompleted;
        }

然后,每次引发CoreWebView2.NewWindowRequested事件时,您将使用此自定义webview 2创建新的webview 2对象。因此,要处理此事件:

private void WebViewInTab_CoreWebView2InitializationCompleted(object? sender, Microsoft.Web.WebView2.Core.CoreWebView2InitializationCompletedEventArgs e)
{
    CoreWebView2.NewWindowRequested += CoreWebView2_NewWindowRequested; // This is the man
    CoreWebView2.DocumentTitleChanged += CoreWebView2_DocumentTitleChanged; // Just cosmetic code
}

之后,您将自己处理新窗口,即,只需将带有我们的自定义webview 2的新TabPage控件添加到TabControlTabPages集合中。当然,不要忘记处理程序参数中的Uri

private void CoreWebView2_NewWindowRequested(object? sender, Microsoft.Web.WebView2.Core.CoreWebView2NewWindowRequestedEventArgs e)
    {
        e.Handled = true; // let the default new window 

        TabPage tpage = new TabPage(); // boy

        tpage.Controls.Add(new WebViewInTab(tabCtrl) { Source = new Uri(e.Uri)}); // toy

        tabCtrl.TabPages.Add(tpage); // daddy
        tabCtrl.SelectedTab = tpage; // user expectation
    }

//Just cosmetic code
private void CoreWebView2_DocumentTitleChanged(object? sender, object e)
    {
        int last = tabCtrl.TabPages.Count - 1;
        tabCtrl.TabPages[last].Text = CoreWebView2.DocumentTitle
    }
}

最后,:)在主应用程序窗体构造函数中启动recursion-ready操作。

public Form1()
        {
            InitializeComponent();
            string uriAdd = "https://www.w3schools.com/";
            tabControl1.TabPages[0].Controls.Add(new WebViewInTab(tabControl1) { Source = new Uri(uriAdd) });
        }

相关问题