WPF HyperLink打开嵌入在程序中的WebBrowser

时间:2011-02-13 02:36:33

标签: c# .net wpf webbrowser-control

我的程序中包含以下用于WPF库的HtmlTextBlock:http://www.codeproject.com/KB/WPF/htmltextblock.aspx

现在,我已经有了以下代码来实现HtmlTextBlock:

    private void UserControl_Loaded(object sender, System.Windows.RoutedEventArgs e)
    {
        this.tweeter.Content = twt.User.Username;
    //AddHandler(Hyperlink.ClickEvent, (RoutedEventHandler)Hyperlink_Click);
    ImageSourceConverter conv = new ImageSourceConverter();
    this.tweetImage.Source = (ImageSource)conv.ConvertFromString(twt.User.AvatarURL);
    string txt = twt.Text;
    Regex regx = new Regex("(http|ftp|https)://([\\w+?\\.\\w+])+([a-zA-Z0-9\\~\\!\\@\\#\\$\\%\\^\\&\\*\\(\\)_\\-\\=\\+\\\\\\/\\?\\.\\:\\;\\'\\,]*)?", RegexOptions.IgnoreCase);
    MatchCollection matches = regx.Matches(txt);
    foreach (Match m in matches)
    {
        int strt = txt.IndexOf(m.Value);
        int end = strt + m.Value.Length;
        if (strt != -1)
        {
            txt = txt.Insert(end, "[/a]");
            txt = txt.Insert(strt, "[a href=" + m.Value + "]");
        }
    }

    this.tweetText.Html = txt;
    string source = "web";
    if (twt.SourceName != null)
        source = twt.SourceName;
    string dateString = twt.DatePosted;
    const string format = "ddd MMM dd HH:mm:ss zzzz yyyy";
    DateTime my_date = DateTime.ParseExact(dateString, format, CultureInfo.InvariantCulture);
    TimeSpan ts = new TimeSpan();
    ts = DateTime.Now - my_date;
    string date = "date";

    // Date Parsing
    if (ts.Days > 0)
    {
        string month = "Mo";
        switch (my_date.Month)
        {
            case 1:
                month = "Jan";
                break;
            case 2:
                month = "Feb";
                break;
            case 3:
                month = "Mar";
                break;
            case 4:
                month = "Apr";
                break;
            case 5:
                month = "May";
                break;
            case 6:
                month = "June";
                break;
            case 7:
                month = "July";
                break;
            case 8:
                month = "Aug";
                break;
            case 9:
                month = "Sep";
                break;
            case 10:
                month = "Oct";
                break;
            case 11:
                month = "Nov";
                break;
            case 12:
                month = "Dec";
                break;
        }
        date = String.Format("on {0}, {1} {2}, {3}", my_date.DayOfWeek.ToString(), month, my_date.Day, my_date.Year);
    }
    else if (ts.Hours > 0)
        if (ts.Hours == 1)
            date = "1 hour ago";
        else
            date = string.Format("{0} hours ago", ts.Hours);
    else if (ts.Minutes > 0)
        if (ts.Minutes == 1)
            date = "1 minute ago";
        else
            date = string.Format("{0} minutes ago", ts.Minutes);
    else if (ts.Seconds > 30)
        date = string.Format("{0} seconds ago", ts.Seconds);
    else
        date = "just now";

    this.sourceLabel.Content = String.Format("posted {0} from {1}", date, source);

    //tweetText.ContextMenu.StaysOpen = false;
    }

private void Button_Click(object sender, RoutedEventArgs e)
{
    Clipboard.SetText(tweetText.Text);
}

private void Hyperlink_Click(object sender, RoutedEventArgs e)
{
    if (e.OriginalSource is Hyperlink)
    {
        Process.Start((e.OriginalSource as Hyperlink).NavigateUri.ToString());
        e.Handled = true;
    }
}

这里的问题是,当单击其中一个超链接时,程序会在默认浏览器中打开链接,这由代码中显示的EventHandlers确定。但是,它还会打开嵌入程序本身的WebBrowser控件。

我已经搜索过源代码,但我发现没有任何内容包含“WebBrowser”或类似内容,所以我认为它不是库。

可能是WPF问题吗?难道我做错了什么?它变得非常令人沮丧。

编辑:刚修复此问题。这是因为我使用的是ClickEvent而不是RequestNavigateEvent。

1 个答案:

答案 0 :(得分:0)

以下行将在默认浏览器中打开URL

Process.Start((e.OriginalSource as Hyperlink).NavigateUri.ToString());

删除它应该会停止浏览器打开。要在Web浏览器控件中打开uri,您需要设置source属性。

webBrowser.Source = (e.OriginalSource as Hyperlink).NavigateUri.ToString();