2011-08-03 185 views
0

我使用自定義的文本塊在我的WPF應用程序,當我在WPF Windows中使用它,它的工作很好,但是當我在一個WPF頁面中使用它,它使一個問題。當我點擊我的自定義控件一個鏈接就可以瀏覽的鏈接,並顯示在瀏覽器,但在WPF頁面導航回到另一個WPF頁面過(第一頁)WPF頁面導航

namespace Dtwitter.Controls 
{ 

public class TweetTextBlock : TextBlock 
{ 

    public TweetTextBlock() 
    { 

    } 

    #region Dependency properties 

    public string TweetText 
    { 
     get { return (string)GetValue(TweetTextProperty); } 
     set { SetValue(TweetTextProperty, value); } 
    } 

    // Using a DependencyProperty as the backing store for TweetText. This enables animation, styling, binding, etc... 
    public static readonly DependencyProperty TweetTextProperty = 
     DependencyProperty.Register("TweetText", typeof(string), typeof(TweetTextBlock), 
     new FrameworkPropertyMetadata(string.Empty, new PropertyChangedCallback(OnTweetTextChanged))); 

    #endregion 



    private static void OnTweetTextChanged(DependencyObject obj, DependencyPropertyChangedEventArgs args) 
    { 
     string text = args.NewValue as string; 
     if (!string.IsNullOrEmpty(text)) 
     { 
      TweetTextBlock textblock = (TweetTextBlock)obj; 
      textblock.Inlines.Clear(); 
      textblock.Inlines.Add(" "); 

      string[] words = Regex.Split(text, @"([ \(\)\{\}\[\]])"); 

      string possibleUserName = words[0].ToString(); 

      if ((possibleUserName.Length > 1) && (possibleUserName.Substring(1, 1) == "@")) 
      { 
       textblock = FormatName(textblock, possibleUserName); 
       words.SetValue("", 0); 
      } 

      foreach (string word in words) 
      { 
       // clickable hyperlinks 
       if (UrlShorteningService.IsUrl(word)) 
       { 
        try 
        { 
         Hyperlink link = new Hyperlink(); 
         link.NavigateUri = new Uri(word); 
         link.Inlines.Add(word); 
         link.Click += new RoutedEventHandler(link_Click); 
         link.ToolTip = "Open link in the default browser"; 
         textblock.Inlines.Add(link); 
        } 
        catch 
        { 
         //TODO:What are we catching here? Why? Log it? 
         textblock.Inlines.Add(word); 
        } 
       } 
       // clickable @name 
       else if (word.StartsWith("@")) 
       { 
        textblock = FormatName(textblock, word); 

       } 

       // clickable #hashtag 
       else if (word.StartsWith("#")) 
       { 
        string hashtag = String.Empty; 
        Match foundHashtag = Regex.Match(word, @"#(\w+)(?<suffix>.*)"); 
        if (foundHashtag.Success) 
        { 
         hashtag = foundHashtag.Groups[1].Captures[0].Value; 
         Hyperlink tag = new Hyperlink(); 
         tag.Inlines.Add(hashtag); 

         string hashtagUrl = "http://search.twitter.com/search?q=%23{0}"; 

         // The main application has access to the Settings class, where a 
         // user-defined hashtagUrl can be stored. This hardcoded one that 
         // is used to set the NavigateUri is just a default behavior that 
         // will be used if the click event is not handled for some reason. 

         tag.NavigateUri = new Uri(String.Format(hashtagUrl, hashtag)); 
         tag.ToolTip = "Show statuses that include this hashtag"; 
         tag.Tag = hashtag; 

         tag.Click += new RoutedEventHandler(hashtag_Click); 

         textblock.Inlines.Add("#"); 
         textblock.Inlines.Add(tag); 
         textblock.Inlines.Add(foundHashtag.Groups["suffix"].Captures[0].Value); 
        } 
       } 
       else 
       { 
        textblock.Inlines.Add(word); 
       } 
      } 

      textblock.Inlines.Add(" "); 
     } 
    } 

    public static TweetTextBlock FormatName(TweetTextBlock textblock, string word) 
    { 
     string userName = String.Empty; 
     string firstLetter = word.Substring(0, 1); 

     Match foundUsername = Regex.Match(word, @"@(\w+)(?<suffix>.*)"); 

     if (foundUsername.Success) 
     { 
      userName = foundUsername.Groups[1].Captures[0].Value; 
      Hyperlink name = new Hyperlink(); 
      name.Inlines.Add(userName); 
      name.NavigateUri = new Uri("http://twitter.com/" + userName); 
      name.ToolTip = "View @" + userName + "'s recent tweets"; 
      name.Tag = userName; 

      name.Click += new RoutedEventHandler(name_Click); 

      if (firstLetter != "@") 
       textblock.Inlines.Add(firstLetter); 

      textblock.Inlines.Add("@"); 
      textblock.Inlines.Add(name); 
      textblock.Inlines.Add(foundUsername.Groups["suffix"].Captures[0].Value); 
     } 
     return textblock; 
    } 


    static void link_Click(object sender, RoutedEventArgs e) 
    { 
     try 
     { 
      System.Diagnostics.Process.Start(((Hyperlink)sender).NavigateUri.ToString()); 
     } 
     catch 
     { 
      //TODO: Log specific URL that caused error 
      MessageBox.Show("There was a problem launching the specified URL.", "Error", MessageBoxButton.OK, MessageBoxImage.Exclamation); 
     } 
    } 

} 

}

+0

我更正了一些拼寫和主題標題。請嘗試使用正確的拼寫,並嘗試更好地解釋您的問題,而不是「出現問題」。請仔細閱讀本:http://msmvps.com/blogs/jon_skeet/archive/2010/08/29/writing-the-perfect-question.aspx –

回答

1

改變你的鏈接點擊的方法到

static void link_click(Object sender, RequestNavigateEventArgs e) { 
    try { 
     System.Diagnostics.Process.Start(e.Uri.ToString()); 
    } catch { 
     //TODO: Log specific URL that caused error 
     MessageBox.Show("There was a problem launching the specified URL.", "Error", MessageBoxButton.OK, MessageBoxImage.Exclamation); 
    } finally { 
     e.Handled = true; 
    } 
} 

更改

link.Click+=new RoutedEventHandler(link_Click); 

link.RequestNavigate+=new RequestNavigateEventHandler(link_Click); 

在link_click中設置e.Handled=true以表示您已處理鏈接點擊以防止框架額外處理您的鏈接點擊進一步。

或者你可以只設置超鏈接的targetName屬性爲「_blank」,而不是需要過程啓動命令

+0

我測試它,但它不工作:( – ArMaN

+0

WPF頁自動導航!當Brwoser網頁加載 – ArMaN

+0

更新的答案,你需要一個不同的事件 –

0

下面的代碼應該使其工作以同樣的方式在這兩種情況下(頁和窗口) ....

嘗試這種在超鏈接對象的MouseDown打開網絡瀏覽器的超鏈接。

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

讓我知道這是否有幫助。

+0

我測試它,但它不工作:( – ArMaN

+0

WPF頁面自動導航e當網頁加載Brwoser時! – ArMaN

+0

嘗試Hyperlink.PreviewMouseDown事件處理程序中的上述代碼... –