zoukankan      html  css  js  c++  java
  • WP&Win10开发: RichTextBlock实现富文本并处理换行

    思路
    1.构建字典。
    2.在字符串中匹配字典的key,将匹配到的key转换成对应的value
    3.将替换后的字符串,转化成xaml形式,加载该xaml以实现富文本。

    代码如下

    private Paragraph getRichText(string richText)
            {
                var r = new Regex(builder.ToString()); //获取正则。
                var mc = r.Matches(richText); //匹配富文本,获取匹配到的集合。
                foreach (Match m in mc) //遍历集合将richText中所有的值转换成xaml的形式。
                {
                    //string.Format 中的内容不要出现换行符,否则会出现换行出错。
                    richText = richText.Replace(m.Value, string.Format(@"<InlineUIContainer><Border><Image Source=""ms-appx:///Assets/Emoji/{0}.png"" Width=""30"" Height=""30""/></Border></InlineUIContainer>", emojiDict[m.Value]));
                }
                richText = richText.Replace("
    ", "<LineBreak/>"); //将换行符转换成<LineBreak/>,用于实现换行。
    
                //生成xaml
                var xaml = string.Format(@"<Paragraph 
                                            xmlns=""http://schemas.microsoft.com/winfx/2006/xaml/presentation""
                                            xmlns:x=""http://schemas.microsoft.com/winfx/2006/xaml"">
                                        <Paragraph.Inlines>
                                        <Run></Run>
                                          {0}
                                        </Paragraph.Inlines>
                                    </Paragraph>", richText);
                var p = (Paragraph)XamlReader.Load(xaml);
                p.Foreground = new SolidColorBrush(Color.FromArgb(255, 255, 255, 255));
                return p;
            }


    Demo分享链接:http://pan.baidu.com/s/1hqGVOcW

    运行图如下


    有问题请加群:53078485

  • 相关阅读:
    Tomcat造成404
    ajax缺少@ResponseBody注解前台404,业务可以运行
    几种常见的Runtime Exception
    SQL注入通俗讲解
    MYSQL数据库导入大数据量sql文件失败的解决方案
    css选择器
    http端口
    基础算法之最大子列求和问题
    基础算法之链表逆序
    Prolog&Epilog
  • 原文地址:https://www.cnblogs.com/clever-he/p/4775615.html
Copyright © 2011-2022 走看看