Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/apache/8.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Windows phone 7 将带有链接的文本绑定到RichTextBox_Windows Phone 7_Richtextbox - Fatal编程技术网

Windows phone 7 将带有链接的文本绑定到RichTextBox

Windows phone 7 将带有链接的文本绑定到RichTextBox,windows-phone-7,richtextbox,Windows Phone 7,Richtextbox,我需要将可能包含超链接的文本绑定到RichTextBox,以便它可以将文本显示为普通文本,将链接显示为超链接 例如,我有以下文本: Join us on social networks http://www.facebook.com/ 我希望文本中的链接是超链接,因此RichTextBox中的结果如下: 加入我们的社交网络 解析超链接,并创建以下结构(当然,使用C#): 你好,世界! http://www.stackoverflow.com 我实现了我需要的 using System; us

我需要将可能包含超链接的文本绑定到RichTextBox,以便它可以将文本显示为普通文本,将链接显示为超链接

例如,我有以下文本:

Join us on social networks
http://www.facebook.com/
我希望文本中的链接是超链接,因此RichTextBox中的结果如下:

加入我们的社交网络


解析超链接,并创建以下结构(当然,使用C#):


你好,世界!
http://www.stackoverflow.com

我实现了我需要的

using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Text.RegularExpressions;
using System.Windows.Media;

namespace NazarGrynko.UI.Controls
{
    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 = "_blank",
                                            Foreground = Application.Current.Resources["PhoneAccentBrush"] as Brush
                                        };
                    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 = "_blank",
                                            Foreground = Application.Current.Resources["PhoneAccentBrush"] as Brush
                                        };
                    hyperlink.Inlines.Add(urlMatch.Value);
                    paragraph.Inlines.Add(hyperlink);
                    textPosition += urlMatch.Value.Length;
                }
            }

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

            richTextBox.Blocks.Add(paragraph);
        }
    }
}
使用示例:

<MyRichTextBox Text="{Binding Message}"/>

谢谢您的解决方案

最后我做了一个小小的修改,我用一行只添加全文的子字符串替换了计数检查,这样它就不会截断最后一个URL之后的所有内容,所有文本都会保留

        paragraph.Inlines.Add(text.Substring(textPosition, text.Length - textPosition));

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

亲爱的上帝,谢谢你,非常感谢你!你救了我的命。无论如何,我认为你在声明DependencyProperty TextProperty时写的是“typeof(MediaDownloaderRichTextBox)”而不是“typeof(MyRichTextBox)”@是的,我忘了更改这个,那是一个原始控件名。很高兴我帮了你Nazar当超链接出现在文本开头时,你忘了添加段落。Inlines.add(超链接);感谢您提供的解决方案(顺便说一句;)@我已经纠正了这个错误。在我注意到你的评论之前,我也遇到了它。
        paragraph.Inlines.Add(text.Substring(textPosition, text.Length - textPosition));

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