自定义超链接类 [英] Custom Hyperlink class

查看:75
本文介绍了自定义超链接类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好

我正在开发Windows手机,

I am developing for windows phone,

我必须显示一个文本,其中包含一些文字和一些网址,我必须将文本显示为普通文本,将url显示为超链接URL。

I have to show a text, which have some text and some url, I have to show the text as normal text and url as hyperlink url.

我找到了这个自定义RichTextbox,这很好用

I found this custom RichTextbox , and this works fine

 public class MyRichTextBox : RichTextBox
    {
        private const string UrlPattern = @"(http|ftp|https):\/\/[\w\-_]+(\.[\w\-_]+)+([\w\-\.,@?^=%&:/~\+#]*[\w\-\@?^=%&/~\+#])?";
        public static readonly DependencyProperty TextProperty =
            DependencyProperty.Register("Text", typeof(string), typeof(MyRichTextBox), new PropertyMetadata(default(string), TextPropertyChanged));


        public string Text
        {
            get { return (string)GetValue(TextProperty); }
            set { SetValue(TextProperty, value); }
        }

        private static void TextPropertyChanged(DependencyObject dependencyObject, DependencyPropertyChangedEventArgs dependencyPropertyChangedEventArgs)
        {
            var richTextBox = (MyRichTextBox)dependencyObject;
            var text = (string)dependencyPropertyChangedEventArgs.NewValue;
            int textPosition = 0;
            var paragraph = new Paragraph();

            var urlMatches = Regex.Matches(text, UrlPattern);

            foreach (Match urlMatch in urlMatches)
            {
                int urlOccurrenceIndex = text.IndexOf(urlMatch.Value, textPosition, StringComparison.Ordinal);

                if (urlOccurrenceIndex == 0)
                {
                    var hyperlink = new Hyperlink
                                        {
                                            NavigateUri = new Uri(urlMatch.Value),
                                            TargetName = "_self",
                                            Foreground = Application.Current.Resources["PhoneAccentBrush"] as Brush
                                        };
                    hyperlink.Click += hyperlink_Click;
                    hyperlink.Inlines.Add(urlMatch.Value);

                    paragraph.Inlines.Add(hyperlink);
                    textPosition += urlMatch.Value.Length;
                }
                else
                {
                    paragraph.Inlines.Add(text.Substring(textPosition, urlOccurrenceIndex - textPosition));
                    textPosition += urlOccurrenceIndex - textPosition;
                    var hyperlink = new Hyperlink
                                        {
                                            NavigateUri = new Uri(urlMatch.Value),
                                            TargetName = " _self",
                                            Foreground = Application.Current.Resources["PhoneAccentBrush"] as Brush

                                        };

                    hyperlink.Inlines.Add(urlMatch.Value);
                    hyperlink.Click += hyperlink_Click;

                    paragraph.Inlines.Add(hyperlink);
                    textPosition += urlMatch.Value.Length;
                }
            }

            if (urlMatches.Count == 0)
            {
                paragraph.Inlines.Add(text);
            }

            richTextBox.Blocks.Add(paragraph);
        }

        static void hyperlink_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                Hyperlink hp = sender as Hyperlink;
                string url = hp.NavigateUri.ToString();
                NavigationHelper.getInstance().changeState(url);
            }
            catch (Exception k)
            {
            }
        }

这就是我的问题,我想要在单击超链接时手动执行操作,我不希望这在默认浏览器中自动打开URL。

Here what my problem is, I want to do the action manually when the hyperlink clicked, I don't want this to open the url automatically in a default browser.

我处理了RichTextbox中使用的此超链接的click事件,但是,当我点击网址时,它会在新的原生浏览器中打开网址。

I handled the click event of this hyperlink used in the RichTextbox, but still, when i click the url , it opens the url in a new native browser.

任何人都可以帮我这样做,我想在新浏览器中打开网址。

Can anybody help me to do this, I want to make false the url open in new browser.

任何人都可以给我一些自定义超链接的代码,它不会在新浏览器中打开网址。

Can anybody give me some code for custom hyperlink which doesn't open the url in new browser.

谢谢。

推荐答案

在这种情况下,请勿指定NavigateUri。 只需指定一个点击处理程序,并将您的逻辑放在那里,以了解您想要做什么。 由于您在代码中执行此操作,因此为您正在解析的Uri分配内联事件处理程序会很简单。

Don't assign the NavigateUri in this case.  Simply assign a click handler and put your logic in there as to what you want to do.  Since you are doing this all in code it would be simple to assign an inline event handler for the Uri you are parsing.


这篇关于自定义超链接类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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