WebBrowser-Control - open default browser on click on link

后端 未结 3 890
隐瞒了意图╮
隐瞒了意图╮ 2021-02-04 19:49

I use the WebBrowser-Control in my WPF-Application like

    

Now, when I navigat

3条回答
  •  情歌与酒
    2021-02-04 20:01

    You can open the new page in default browser using Proces.Start() on Navigating event and set e.Cancel = true; so that the page in the control will not change.

    Example:

    @MainWindow.xaml.cs

    using System.Diagnostics;
    using System.Windows;
    using System.Windows.Navigation;
    
    namespace OpenDefaultBrowser
    {
        public partial class MainWindow : Window
        {
            private static bool willNavigate;
    
            public MainWindow()
            {
                InitializeComponent();
            }
    
            private void webBrowser1_Navigating(object sender, NavigatingCancelEventArgs e)
            {
                // first page needs to be loaded in webBrowser control
                if (!willNavigate)
                {
                    willNavigate = true;
                    return;
                }
    
                // cancel navigation to the clicked link in the webBrowser control
                e.Cancel = true;
    
                var startInfo = new ProcessStartInfo
                {
                    FileName = e.Uri.ToString()
                };
    
                Process.Start(startInfo);
            }
        }
    }
    

    @MainWindow.xaml

    
        
            
        
    
    

提交回复
热议问题