2011-02-13 51 views
1

內的web瀏覽器我有一個WPF庫以下HtmlTextBlock列入我的計劃之內:http://www.codeproject.com/KB/WPF/htmltextblock.aspxWPF打開超鏈接的嵌入式程序

現在,我已經得到了是應該執行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。

+0

你說,你想讓它打開默認瀏覽器,並正在創建和放置的地方您的WPF窗口上的Web瀏覽器控件(覆蓋整個事情?),並導航到該頁面? – 2011-02-13 09:15:29

+0

是的,我希望它在默認瀏覽器中打開,但它同時具有這兩種功能 - 在默認瀏覽器中打開並創建Web瀏覽器控件並將其放置在整個窗口中。 – sammarks 2011-02-13 16:31:51

回答

0

在默認瀏覽器

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

將打開URL刪除應該停止瀏覽器中打開下面的一行。要在Web瀏覽器控件中打開uri,您需要設置源屬性。

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