2011-08-03 271 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事件处理程序中的上述代码... –