コード例 #1
0
ファイル: RichTextExtended.cs プロジェクト: cmpute/XiamiTing
 internal static void Analyse(HtmlNode node, InlineCollection container, ParsingStyle style)
 {
     if (node == null)
         return;
     foreach (var item in node.ChildNodes)
     {
         switch (item.Name)
         {
             //文本
             case "#text":
                 container.Add(new Run() { Text = item.InnerText });
                 continue;
             case "strong":
                 style = style | ParsingStyle.Bold;
                 break;
             //链接
             case "a":
                 var link = new Hyperlink();
                 ParseRelativeUrl(item.GetAttributeValue("href", ""), link, App.Current.NavigationService);
                 Analyse(item, link.Inlines, style);
                 container.Add(link);
                 continue;
             //图片
             case "img":
                 var image = new Image() { Source = new BitmapImage(new Uri(item.GetAttributeValue("src", ""))) };
                 var cont = new InlineUIContainer();
                 cont.Child = image;
                 container.Add(cont);
                 continue;
             //换行
             case "br":
                 container.Add(new LineBreak());
                 continue;
             //容器
             case "span":
             case "div":
             case "p":
                 break;
             case "b":
                 //TODO: 设置属性,是否要加红搜索显示结果
                 if(node.GetAttributeValue("class","") == "key_red")
                 {
                     Span p = new Span();
                     p.Foreground = new SolidColorBrush(Colors.Red);
                     Analyse(item, container, style);
                     continue;
                 }
                 else break;
             //非文本
             case "button":
                 continue;
             default:
                 continue;
         }
         Analyse(item, container, style);
     }
 }
コード例 #2
0
        internal static void Analyse(HtmlNode node, InlineCollection container, ParsingStyle style)
        {
            if (node == null)
            {
                return;
            }
            foreach (var item in node.ChildNodes)
            {
                switch (item.Name)
                {
                //文本
                case "#text":
                    container.Add(new Run()
                    {
                        Text = item.InnerText
                    });
                    continue;

                case "strong":
                    style = style | ParsingStyle.Bold;
                    break;

                //链接
                case "a":
                    var link = new Hyperlink();
                    ParseRelativeUrl(item.GetAttributeValue("href", ""), link, App.Current.NavigationService);
                    Analyse(item, link.Inlines, style);
                    container.Add(link);
                    continue;

                //图片
                case "img":
                    var image = new Image()
                    {
                        Source = new BitmapImage(new Uri(item.GetAttributeValue("src", "")))
                    };
                    var cont = new InlineUIContainer();
                    cont.Child = image;
                    container.Add(cont);
                    continue;

                //换行
                case "br":
                    container.Add(new LineBreak());
                    continue;

                //容器
                case "span":
                case "div":
                case "p":
                    break;

                case "b":
                    //TODO: 设置属性,是否要加红搜索显示结果
                    if (node.GetAttributeValue("class", "") == "key_red")
                    {
                        Span p = new Span();
                        p.Foreground = new SolidColorBrush(Colors.Red);
                        Analyse(item, container, style);
                        continue;
                    }
                    else
                    {
                        break;
                    }

                //非文本
                case "button":
                    continue;

                default:
                    continue;
                }
                Analyse(item, container, style);
            }
        }