WebBrowser-Control-在单击链接时打开默认浏览器 [英] WebBrowser-Control - open default browser on click on link

查看:46
本文介绍了WebBrowser-Control-在单击链接时打开默认浏览器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在

    <WebBrowser x:Name="webBrowser" Margin="0,28,0,0" />

现在,当我导航到包含链接的mht页并且用户单击该链接之一时,将在WebBrowser-Control中打开新页面.但是应该在新的Default-Browser-Window中打开它.WebBrowser-Control中的内容不应更改.有没有办法改变这种行为?

Now, when I navigate to a mht-page which contains links and the user click on one of this link, the new page is opened in the WebBrowser-Control. But it should be opened in a new Default-Browser-Window. The content in the WebBrowser-Control should not be changed. Is there a way to change this behavior?

推荐答案

您可以使用

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.

示例:

@ MainWindow.xaml.cs

@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

@MainWindow.xaml

<Window x:Class="OpenDefaultBrowser.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="464" Width="1046">
    <Grid>
        <WebBrowser Height="425" HorizontalAlignment="Left" Name="webBrowser1" VerticalAlignment="Top" Width="1024" Source="http://stackoverflow.com/" Navigating="webBrowser1_Navigating" />
    </Grid>
</Window>

这篇关于WebBrowser-Control-在单击链接时打开默认浏览器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆