Exemplo n.º 1
0
        private void SetColorText(string htmlFragment)
        {
            if (htmlFragment == null || htmlFragment.Length == 0)
            {
                return;
            }

            Paragraph paragraph;

            if (this.Blocks.Count == 0 ||
                (paragraph = this.Blocks[this.Blocks.Count - 1] as Paragraph) == null)
            {
                paragraph = new Paragraph();
                this.Blocks.Add(paragraph);
            }
            var nextOffset = 0;

            var regEx = new Regex(@"(\[color=#FFFFFF\](?<colortxt>.*?)\[/color\])", RegexOptions.IgnoreCase | RegexOptions.Singleline);

            foreach (Match match in regEx.Matches(htmlFragment))
            {
                if (match.Index == nextOffset)
                {
                    nextOffset = match.Index + match.Length;
                    paragraph.Inlines.Add(new Run {
                        Text = "(此处反白)->" + HtmlHelp.NoTag(match.Groups["colortxt"].Value) + "<-", Foreground = new SolidColorBrush(Colors.Red)
                    });
                }
                else if (match.Index > nextOffset)
                {
                    this.SetImgText(htmlFragment.Substring(nextOffset, match.Index - nextOffset));
                    nextOffset = match.Index + match.Length;
                    paragraph.Inlines.Add(new Run {
                        Text = "(此处反白)->" + HtmlHelp.NoTag(match.Groups["colortxt"].Value) + "<-", Foreground = new SolidColorBrush(Colors.Red)
                    });
                }
            }

            if (nextOffset < htmlFragment.Length)
            {
                this.SetImgText(htmlFragment.Substring(nextOffset));
            }
        }
Exemplo n.º 2
0
        private void SetImgText(string htmlFragment)
        {
            if (htmlFragment == null || htmlFragment.Length == 0)
            {
                return;
            }

            Paragraph paragraph;

            if (this.Blocks.Count == 0 ||
                (paragraph = this.Blocks[this.Blocks.Count - 1] as Paragraph) == null)
            {
                paragraph = new Paragraph();
                this.Blocks.Add(paragraph);
            }
            var nextOffset = 0;

            var regEx = new Regex(@"(\[img=(?<imgname>.*?)\](?<imgtxt>.*?)\[/img\])", RegexOptions.IgnoreCase | RegexOptions.Singleline);

            foreach (Match match in regEx.Matches(htmlFragment))
            {
                if (match.Index == nextOffset)
                {
                    nextOffset = match.Index + match.Length;

                    var run = new Run {
                        Text = "[此处有图片]", Foreground = new SolidColorBrush(Color.FromArgb(255, 150, 230, 230))
                    };
                    var link = new Hyperlink {
                    };
                    link.Click += new RoutedEventHandler((sender, e) =>
                    {
                        Navi.NavigationTo("/ImageView.xaml?url=" + HttpUtility.UrlEncode(match.Groups["imgtxt"].Value));
                    });
                    link.Inlines.Add(run);
                    paragraph.Inlines.Add(link);
                }
                else if (match.Index > nextOffset)
                {
                    this.SetImgText(htmlFragment.Substring(nextOffset, match.Index - nextOffset));
                    nextOffset = match.Index + match.Length;

                    var run = new Run {
                        Text = "[此处有图片]", Foreground = new SolidColorBrush(Color.FromArgb(255, 150, 230, 230))
                    };
                    var link = new Hyperlink {
                    };
                    link.Click += new RoutedEventHandler((sender, e) =>
                    {
                        Navi.NavigationTo("/ImageView.xaml?url=" + HttpUtility.UrlEncode(match.Groups["imgtxt"].Value));
                    });
                    link.Inlines.Add(run);
                    paragraph.Inlines.Add(link);
                }
            }

            if (nextOffset < htmlFragment.Length)
            {
                paragraph.Inlines.Add(new Run {
                    Text = HtmlHelp.NoTag(htmlFragment.Substring(nextOffset))
                });
            }
        }