Пример #1
0
        public void AppendLink(string text, Uri uri)
        {
            Paragraph paragraph;

            if (this.Blocks.Count == 0 ||
                (paragraph = this.Blocks[this.Blocks.Count - 1] as Paragraph) == null)
            {
                paragraph = new Paragraph();
                this.Blocks.Add(paragraph);
                paragraph.Inlines.Add(new Run {
                    Text = "    "
                });
            }

            var run = new Run {
                Text = HtmlHelp.NoHTML(text)
            };
            var link = new Hyperlink {
            };

            link.Click += new RoutedEventHandler((sender, e) =>
            {
                var task = new WebBrowserTask();
                task.Uri = uri;
                task.Show();
            });

            link.Inlines.Add(run);
            paragraph.Inlines.Add(link);
        }
        List <string> GetHtmlContent(string strhtml)
        {
            var list  = new List <string>();
            var regEx = new Regex(@"\<img\s(src\=""|[^\>]+?\ssrc\="")(?<img>[^""]+)"".*?>",
                                  RegexOptions.IgnoreCase | RegexOptions.Multiline);

            var nextOffset = 0;

            foreach (Match match in regEx.Matches(strhtml))
            {
                if (match.Index == nextOffset)
                {
                    nextOffset = match.Index + match.Length;
                    list.Add(match.Groups["img"].Value);
                }
                else if (match.Index > nextOffset)
                {
                    list.Add(HtmlHelp.NoHTMLContent(strhtml.Substring(nextOffset, match.Index - nextOffset)));

                    nextOffset = match.Index + match.Length;

                    list.Add(match.Groups["img"].Value);
                }
            }

            if (nextOffset < strhtml.Length)
            {
                list.Add(HtmlHelp.NoHTMLContent(strhtml.Substring(nextOffset)));
            }

            return(list);
        }
Пример #3
0
        private async Task <ObservableCollection <ACItem> > getList(ObservableCollection <ACItem> list, string url)
        {
            try
            {
                indicator.IsVisible = true;
                var request = (HttpWebRequest)WebRequest.Create(new Uri(url));
                request.UserAgent = @"Mozilla/5.0 (compatible; MSIE 10.0; Windows NT 6.1; Trident/6.0)";
                var response = await request.GetResponseAsync();

                var doc = new HtmlDocument();
                doc.Load(response.GetResponseStream());


                var node = getListNode(doc);
                if (node != null)
                {
                    foreach (var item in node.ChildNodes)
                    {
                        try
                        {
                            if (item.Name.Equals("div"))
                            {
                                var listtemp = new List <HtmlNode>();
                                foreach (var label in item.ChildNodes[0].ChildNodes)
                                {
                                    if (label.Name.Equals("a") || label.Name.Equals("p") || label.Name.Equals("div"))
                                    {
                                        listtemp.Add(label);
                                    }
                                }

                                ACItem acitem = new ACItem();
                                acitem.title  = listtemp[0].InnerText;
                                acitem.href   = listtemp[0].Attributes[0].Value;
                                acitem.dis    = listtemp[1].ChildNodes[2].InnerText;
                                acitem.time   = listtemp[0].Attributes[2].Value;
                                acitem.name   = HtmlHelp.NoHTML(listtemp[1].ChildNodes[0].InnerText);
                                acitem.beizhu = HtmlHelp.NoHTML(listtemp[2].InnerText);
                                list.Add(acitem);
                            }
                        }
                        catch (Exception ex)
                        {
                            Debug.WriteLine(ex.StackTrace);
                        }
                    }
                }
                indicator.IsVisible = false;
            }
            catch (WebException) { MessageBox.Show("网络不给力啊~"); }
            catch (Exception) { }
            indicator.IsVisible = false;
            return(list);
        }
Пример #4
0
        public void AppendText(string text)
        {
            Paragraph paragraph;

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

            paragraph.Inlines.Add(new Run {
                Text = HtmlHelp.NoHTML(text)
            });
        }
Пример #5
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);
            }
            int 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)
                {
                    paragraph.Inlines.Add(new Run {
                        Text = HtmlHelp.NoTag(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)
            {
                paragraph.Inlines.Add(new Run {
                    Text = HtmlHelp.NoTag(htmlFragment.Substring(nextOffset))
                });
            }
        }
Пример #6
0
        private void SetEmojiText(string htmlFragment)
        {
            if (htmlFragment == null || htmlFragment.Length == 0)
            {
                return;
            }

            this.Blocks.Clear();

            Paragraph paragraph;

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

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

            foreach (Match match in regEx.Matches(htmlFragment))
            {
                if (match.Index == nextOffset)
                {
                    nextOffset = match.Index + match.Length;
                    if (match.Groups["emoji"].Value.Split(',').Count() > 1)
                    {
                        Grid grid = new Grid();
                        grid.Background = new SolidColorBrush(Colors.White);
                        grid.Margin     = new Thickness(1, 0, 1, 0);
                        Image       image = new Image();
                        BitmapImage bi    = new BitmapImage(new Uri("/Assets/Emoji/"
                                                                    + match.Groups["emoji"].Value.Split(',')[0] + "/" + match.Groups["emoji"].Value.Split(',')[1] + ".png", UriKind.RelativeOrAbsolute));
                        image.Source = bi;
                        image.Height = 60;
                        grid.Children.Add(image);
                        InlineUIContainer container = new InlineUIContainer();
                        container.Child = grid;
                        paragraph.Inlines.Add(container);
                    }
                    else
                    {
                        this.SetAtText(HtmlHelp.NoHTML(match.Groups["emoji"].Value));
                    }
                }
                else if (match.Index > nextOffset)
                {
                    this.SetAtText(HtmlHelp.NoHTML(htmlFragment.Substring(nextOffset, match.Index - nextOffset)));
                    nextOffset = match.Index + match.Length;
                    if (match.Groups["emoji"].Value.Split(',').Count() > 1)
                    {
                        Grid grid = new Grid();
                        grid.Background = new SolidColorBrush(Colors.White);
                        grid.Margin     = new Thickness(1, 0, 1, 0);
                        Image       image = new Image();
                        BitmapImage bi    = new BitmapImage(new Uri("/Assets/Emoji/"
                                                                    + match.Groups["emoji"].Value.Split(',')[0] + "/" + match.Groups["emoji"].Value.Split(',')[1] + ".png", UriKind.RelativeOrAbsolute));
                        image.Source = bi;
                        image.Height = 60;
                        grid.Children.Add(image);
                        InlineUIContainer container = new InlineUIContainer();
                        container.Child = grid;
                        paragraph.Inlines.Add(container);
                    }
                    else
                    {
                        this.SetAtText(HtmlHelp.NoHTML(match.Groups["emoji"].Value));
                    }
                }
            }

            if (nextOffset < htmlFragment.Length)
            {
                this.SetAtText(HtmlHelp.NoHTML(htmlFragment.Substring(nextOffset)));
            }
        }