用RichTextBox和正则表达式解析微博正文

    public void WeiboContent ( string weiboContent )

    {

               if (weiboContent!=String.empty)
                    {
                        string content = weiboContent;
                        Regex rootRegex = new Regex(@"(((http://)|(ftp://)|(https://)|(zune://)+)[^(\s)]{1,})|(\[{1}\w{0,}[\u4e00-\u9fa5]{0,}\])|(@[^\s]{1,}:)|(#[^\s]{1,}#)");//字符串前加@表示之后的字符串作为正常的字符串解析,不用进行转义
                        string target = content as string;
                        target = HttpUtility.HtmlDecode(target);
                        Paragraph rootParagraph = new Paragraph();
                        Paragraph imgParagraph = new Paragraph();


                        MatchCollection mc = rootRegex.Matches(target);
                        if (mc.Count != 0)
                        {
                            int flag = 0;
                            for (int i = 0; i < mc.Count; i++)
                            {
                                if (mc[i].Index != 0)
                                {
                                    //非匹配内容
                                    Run r = new Run();
                                    r.Text = target.Substring(flag, mc[i].Index - flag);
                                    flag = flag + r.Text.Length;
                                    rootParagraph.Inlines.Add(r);
                                }
                                if (mc[i].Value.StartsWith("#"))
                                {
                                    //#话题#
                                    if (mc[i].Value.Length > 2)
                                    {
                                        Hyperlink h = new Hyperlink();
                                        SolidColorBrush brushTopic = new SolidColorBrush(Colors.Blue);
                                        h.Foreground = brushTopic;
                                        Dictionary<string, string> param = new Dictionary<string, string>();
                                        string text = mc[i].Value.Remove(0, 1);
                                        text = text.Remove(text.Length - 1, 1);
                                        param.Add("query", text);
                                        h.NavigateUri = new Uri("/Detail.xaml?" + param, UriKind.Relative);


                                        Run r = new Run();
                                        r.Text = mc[i].Value;
                                        h.Inlines.Add(r);
                                        rootParagraph.Inlines.Add(h);
                                        flag = flag + mc[i].Value.Length;
                                    }
                                }
                                else if (mc[i].Value.StartsWith("["))
                                {
                                    //[表情]
                                    if (mc[i].Value.Length > 2)
                                    {                                       


                                        Image ImageControl = new Image();
                                        String imageSource = EmotionManager.GetEmotionSource(mc[i].Value);      //获取表情uri
                                        ImageControl.Source = new BitmapImage(new Uri(imageSource, UriKind.Relative));
                                        ImageControl.Stretch = Stretch.Fill;
                                        ImageControl.Width = GetContentWidth("中");
                                        ImageControl.Height = GetContentWidth("中");


                                        InlineUIContainer imgUi = new InlineUIContainer();
                                        imgUi.Child = ImageControl;
                                        rootParagraph.Inlines.Add(imgUi);
                                        flag = flag + mc[i].Value.Length;
                                    }
                                }
                                else if (mc[i].Value.StartsWith("@"))
                                {
                                    //@
                                    Dictionary<string, string> param = new Dictionary<string, string>();
                                    param.Add("content", mc[i].Value);


                                    Hyperlink h = new Hyperlink();
                                    SolidColorBrush brushRefer = new SolidColorBrush(Colors.Blue);
                                    h.Foreground = brushRefer;
                                    h.NavigateUri = new Uri("/Detail.xaml?" + param, UriKind.Relative);


                                    Run r = new Run();
                                    r.Text = mc[i].Value;
                                    h.Inlines.Add(r);
                                    rootParagraph.Inlines.Add(h);
                                    flag = flag + mc[i].Length;
                                }
                                else
                                {
                                    //URL
                                    Hyperlink h = new Hyperlink();
                                    h.TargetName = "_blank";
                                    h.NavigateUri = new Uri(mc[i].Value);
                                    Run r = new Run();
                                    r.Text = mc[i].Value;
                                    h.Inlines.Add(r);
                                    rootParagraph.Inlines.Add(h);
                                    flag = flag + mc[i].Length;
                                }
                            }
                            if (flag < target.Length - 1)
                            {
                                Run r = new Run();
                                r.Text = target.Substring(flag, target.Length - flag);
                                flag = flag + r.Text.Length;
                                rootParagraph.Inlines.Add(r);
                            }


                        }
                        else
                        {
                            rootParagraph.Inlines.Add((new Run()).Text = target);
                        }


                        richTextBox.Blocks.Add(rootParagraph);
                        richTextBox.Blocks.Add(imgParagraph);
                        SolidColorBrush brush=new SolidColorBrush(Colors.Gray);
                        richTextBox.Foreground = brush;
                      

         }

         //获取文本的宽度
        private double GetContentWidth(String aSrc)
        {
            TextBlock text = new TextBlock();
            text.FontSize = contentFontSize;
            text.Text = aSrc;
            return text.ActualWidth;
        }


 参考原文:http://www.wpdevn.com/showtopic-32.aspx  RichTextBox 数据绑定

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值