示例#1
0
        public static void SetLinkedText(this RichTextBox richTextBox, string htmlFragment)
        {
            htmlFragment = cleanHTML(htmlFragment);

            var regEx = new Regex(@"\<a\s(href\=""|[^\>]+?\shref\="")(?<link>[^""]+)"".*?\>(?<text>.*?)(\<\/a\>|$)", RegexOptions.IgnoreCase | RegexOptions.Multiline);

            richTextBox.Blocks.Clear();
            int nextOffset = 0;

            foreach (Match match in regEx.Matches(htmlFragment))
            {
                if (match.Index > nextOffset)
                {
                    richTextBox.AppendText(htmlFragment.Substring(nextOffset, match.Index - nextOffset));
                    nextOffset = match.Index + match.Length;
                    richTextBox.AppendLink(match.Groups["text"].Value, new Uri(match.Groups["link"].Value));
                }
                AppSettings.LogThis(match.Groups["text"] + ":" + match.Groups["link"]);
            }

            if (nextOffset < htmlFragment.Length)
            {
                richTextBox.AppendText(htmlFragment.Substring(nextOffset));
            }
        }