使用winforms c#中的webview2以编程方式打开新选项卡上的弹出链接

gkl3eglg  于 2022-11-17  发布在  C#
关注(0)|答案(1)|浏览(642)

我在该webview中添加了tabcontrol,将其视为wv1。我在该webview中打开一个弹出链接。该弹出链接将在新选项卡和新webview2中打开。
点击下方链接img after run the code wv1 on tab2 image try it button popup link will open on new webview2 inside of a new tab
编码:

namespace WindowsBrowserApplication
{
    public partial class WindowsWebView : Form
    {
        public WindowsWebView()
        {
            InitializeComponent();
        }

        private void WindowsWebView_Load(object sender, EventArgs e)
        {
            InitBrowser();
        }

        private async Task Initizated()
        {
            await WebView.EnsureCoreWebView2Async(null);
        }

        public async void InitBrowser()
        {
            await Initizated();
            WebView.CoreWebView2.Navigate("https://www.w3schools.com/jsref/tryit.asp?filename=tryjsref_win_open");
            WebView.CoreWebView2.NewWindowRequested += CoreWebView2_NewWindowRequested;
        }

        public async void InitBrowserTab()
        {
            await Initizated();
        }
        private void CoreWebView2_NewWindowRequested(object sender, CoreWebView2NewWindowRequestedEventArgs e)
        {
            e.Handled = true;

            InitBrowserTab();
            TabPage tpage = new TabPage();

            var web = new WebView2();
            tpage.Controls.Add(web);
            tabControl.TabPages.Add(tpage);
        }
    }
}
bejyjqdl

bejyjqdl1#

如何让它像真实的的网页浏览器一样工作。简单地,在一个新的标签页中打开。)
首先,创建一个继承自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) });
        }

相关问题