public virtual void RenderElement(IElement element, ITextContainer parent, RenderContextBase context) { var a = (IHtmlAnchorElement)element; if (a.ChildElementCount == 0) { var anchor = new Hyperlink { Foreground = new SolidColorBrush(Colors.LightBlue) }; anchor.Click += async (sender, args) => { var dialog = new MessageDialog(a.Href, "使用浏览器打开"); dialog.Commands.Add(new UICommand("确定", async cmd => { var success = await Launcher.LaunchUriAsync(new Uri(a.Href, UriKind.Absolute)); if (success) { anchor.Foreground = new SolidColorBrush(Colors.Purple); } })); dialog.Commands.Add(new UICommand("取消")); await dialog.ShowAsync(); }; parent.Add(anchor); context.RenderNode(element, new SpanContainer(anchor)); } else { context.RenderNode(element,parent); } }
public virtual void RenderElement(IElement element, ITextContainer parent, RenderContextBase context) { var a = (IHtmlAnchorElement)element; if (a.ChildElementCount == 0) { var anchor = new Hyperlink { Foreground = new SolidColorBrush(Colors.LightBlue) }; anchor.Click += async(sender, args) => { var dialog = new MessageDialog(a.Href, "使用浏览器打开"); dialog.Commands.Add(new UICommand("确定", async cmd => { var success = await Launcher.LaunchUriAsync(new Uri(a.Href, UriKind.Absolute)); if (success) { anchor.Foreground = new SolidColorBrush(Colors.Purple); } })); dialog.Commands.Add(new UICommand("取消")); await dialog.ShowAsync(); }; parent.Add(anchor); context.RenderNode(element, new SpanContainer(anchor)); } else { context.RenderNode(element, parent); } }
public virtual void RenderElement(IElement element, ITextContainer parent, RenderContextBase context) { var richEditBox = new RichEditBox(); richEditBox.Document.SetText(TextSetOptions.None, element.TextContent); parent.Add(richEditBox); }
private static void ParseElement(XmlElement element, ITextContainer parent) { foreach (var child in element.ChildNodes) { if (child is Windows.Data.Xml.Dom.XmlText) { if (string.IsNullOrEmpty(child.InnerText) || child.InnerText == "\n") { continue; } parent.Add(new Run { Text = child.InnerText.Trim(new char[] {'\n'}) }); } else if (child is XmlElement) { XmlElement e = (XmlElement)child; switch (e.TagName.ToUpper()) { case "P": var paragraph = new Paragraph(); parent.Add(paragraph); ParseElement(e, new ParagraphTextContainer(paragraph)); break; case "STRONG": var bold = new Bold(); parent.Add(bold); ParseElement(e, new SpanTextContainer(bold)); break; case "U": var underline = new Underline(); parent.Add(underline); ParseElement(e, new SpanTextContainer(underline)); break; case "A": var inlineElt = new InlineUIContainer(); var hyperlink = new HyperlinkButton(); inlineElt.Child = hyperlink; hyperlink.Style = (Style)App.Current.Resources["HyperlinkButtonStyle"]; hyperlink.Content = e.InnerText; hyperlink.Click += delegate(object sender, RoutedEventArgs eventArgs) { string uriString = e.GetAttribute("href"); Messenger.Default.Send<LinkClickedMessage>(new LinkClickedMessage() { UriToNavigate = uriString }); }; parent.Add(inlineElt); //ParseElement(e, parent); break; case "BR": parent.Add(new LineBreak()); break; } } } }
public virtual void RenderElement(IElement element, ITextContainer parent, RenderContextBase context) { var input = (IHtmlInputElement)element; var type = input.Type ?? string.Empty; type = type.Trim(); if (type.Length == 0) { type = "text"; } type = type.ToLower(); switch (type) { case "text": { var textbox = new TextBox { Text = input.Value }; parent.Add(textbox); break; } case "button": { var button = new Button { Content = input.Value }; parent.Add(button); break; } default: { if (Debugger.IsAttached) { Debugger.Break(); } var textblock = new TextBlock { Text = string.Format("input type=\"{0}\" is not support.", input.Type) }; parent.Add(textblock); break; } } }
public virtual void RenderElement(IElement element, ITextContainer parent, RenderContextBase context) { var button = new Button() { Content = element.TextContent }; parent.Add(button); }
public virtual void RenderElement(IElement element, ITextContainer parent, RenderContextBase context) { var paragraph = new Paragraph() { TextAlignment = TextAlignment.Center }; parent.Add(paragraph); context.RenderNode(element, new ParagraphContainer(paragraph)); }
public virtual void RenderElement(IElement element, ITextContainer parent, RenderContextBase context) { var button = new Button() { Content = element.TextContent }; parent.Add(button); }
public virtual void RenderElement(IElement element, ITextContainer parent, RenderContextBase context) { var paragraph = new Paragraph() { TextAlignment = TextAlignment.Center }; parent.Add(paragraph); context.RenderNode(element, new ParagraphContainer(paragraph)); }
public virtual void RenderElement(IElement element, ITextContainer parent, RenderContextBase context) { var paragraph = new Paragraph { Margin = new Thickness(0, 10, 0, 10) }; parent.Add(paragraph); context.RenderNode(element, new ParagraphContainer(paragraph)); }
public virtual void RenderElement(IElement element, ITextContainer parent, RenderContextBase context) { var line = new Rectangle { MinWidth = double.MaxValue, MinHeight = 1, Fill = new SolidColorBrush(Colors.Gray), Margin = new Thickness(0, 5, 0, 5) }; parent.Add(line); }
public virtual void RenderElement(IElement element, ITextContainer parent, RenderContextBase context) { var audio = (IHtmlAudioElement)element; var mediaElement = new MediaElement { Source = new Uri(audio.Source), AreTransportControlsEnabled = true, MinHeight = 65 }; parent.Add(mediaElement); context.RenderNode(element, parent); }
public virtual void RenderElement(IElement element, ITextContainer parent, RenderContextBase context) { var audio = (IHtmlAudioElement)element; var mediaElement = new MediaElement { Source = new Uri(audio.Source), AreTransportControlsEnabled = true, MinHeight = 65 }; parent.Add(mediaElement); context.RenderNode(element, parent); }
public virtual void RenderElement(IElement element, ITextContainer parent, RenderContextBase context) { var border = new Border { BorderThickness = new Thickness(1), BorderBrush = new SolidColorBrush(Colors.Gray), Margin = new Thickness(0, 5, 0, 5), Padding = new Thickness(10) }; parent.Add(border); var richTextBlock = new RichTextBlock(); border.Child = richTextBlock; context.RenderNode(element, new RichTextBlockContainer(richTextBlock)); }
private static void ParseElement(XmlElement element, ITextContainer parent) { foreach (var child in element.ChildNodes) { if (child is Windows.Data.Xml.Dom.XmlText) { if (string.IsNullOrEmpty(child.InnerText) || child.InnerText == "\n") { continue; } parent.Add(new Run { Text = child.InnerText }); } else if (child is XmlElement) { XmlElement e = (XmlElement)child; switch (e.TagName.ToUpper()) { case "P": var paragraph = new Paragraph(); parent.Add(paragraph); ParseElement(e, new ParagraphTextContainer(paragraph)); break; case "STRONG": var bold = new Bold(); parent.Add(bold); ParseElement(e, new SpanTextContainer(bold)); break; case "U": var underline = new Underline(); parent.Add(underline); ParseElement(e, new SpanTextContainer(underline)); break; case "A": ParseElement(e, parent); break; case "BR": parent.Add(new LineBreak()); break; } } } }
public virtual void RenderElement(IElement element, ITextContainer parent, RenderContextBase context) { var table = (IHtmlTableElement)element; var grid = new Grid(); foreach (var child in table.Rows) { var row = child as IHtmlTableRowElement; if (row != null) { grid.RowDefinitions.Add(new RowDefinition()); int columnCount = 0; foreach (var column in row.Cells) { var richTextBlock = new RichTextBlock() { IsTextSelectionEnabled = false }; var cellRender = new Border() { Child = richTextBlock, Padding = new Thickness(5) }; context.RenderNode(column, new RichTextBlockContainer(richTextBlock)); Grid.SetColumn(cellRender, columnCount); Grid.SetRow(cellRender, grid.RowDefinitions.Count - 1); grid.Children.Add(cellRender); columnCount++; if (grid.ColumnDefinitions.Count < columnCount) { grid.ColumnDefinitions.Add(new ColumnDefinition()); } } } } parent.Add(grid); }
public async void RenderNode(INode node, ITextContainer parentContainer) { var element = node as IElement; if (element != null) { PushTag(element); foreach (var child in node.ChildNodes) { if (child is IText) { var text = child.TextContent ?? string.Empty; if (TagStack.Contains("pre", StringComparer.OrdinalIgnoreCase) == false) { // Not in pre, replace all line break to empty. text = text.Replace("\r", string.Empty).Replace("\n", string.Empty); } if (string.IsNullOrEmpty(text) == false) { parentContainer.Add(new Run { Text = text }); } } else { var childElement = child as IElement; if (childElement != null) { var elementRender = ElementRenders.GetRender(childElement.TagName); if (elementRender != null) { elementRender.RenderElement(childElement, parentContainer, this); } else { if (Debugger.IsAttached) { Debugger.Break(); } await new MessageDialog(string.Format("{0} can't render", childElement.TagName)).ShowAsync(); } } } } PopTag(element); } }
public virtual void RenderElement(IElement element, ITextContainer parent, RenderContextBase context) { var paragraph = new Paragraph(); parent.Add(paragraph); context.RenderNode(element, new OrderListContainer(paragraph)); }
public virtual void RenderElement(IElement element, ITextContainer parent, RenderContextBase context) { var span = new Span(); parent.Add(span); context.RenderNode(element, new SpanContainer(span)); }
private static void ParseElement(XmlElement element, ITextContainer parent) { foreach (var child in element.ChildNodes) { var tagNameToUpper = (child as XmlElement)?.TagName?.ToUpper(); if (!isInP && !(child is XmlElement && (tagNameToUpper == "P" || tagNameToUpper == "SPAN" || tagNameToUpper == "DIV" || tagNameToUpper == "IMG"))) { var paragraph = new Paragraph(); parent.Add(paragraph); isInP = true; parent = new ParagraphTextContainer(parent, paragraph); } if (child is XmlText) { var text = child.InnerText.Replace("\n", "").Trim(); if (string.IsNullOrEmpty(text)) continue; text = text.Replace("</form>", ""); parent.Add(new Run { Text = System.Net.WebUtility.HtmlDecode(text), Foreground = new SolidColorBrush(Windows.UI.Colors.Black) }); } else if (child is XmlElement) { XmlElement e = (XmlElement)child; switch (tagNameToUpper) { case "P": case "DIV": case "SPAN": if(tagNameToUpper == "DIV") { var classVal = e.Attributes.FirstOrDefault(a => a.NodeName == "class")?.NodeValue?.ToString() ?? ""; if(classVal.Contains("open-list-comments") || classVal.Contains("open-list-footer") || classVal.Contains("modal-body")) { return; } } if (isInP) { if (tagNameToUpper == "P") parent.Add(new LineBreak()); ParseElement(e, parent); } else { var paragraph = new Paragraph(); parent.Add(paragraph); isInP = true; ParseElement(e, new ParagraphTextContainer(parent, paragraph)); isInP = false; } break; case "B": case "STRONG": case "H1": case "H2": case "H3": case "H4": case "H5": case "H6": var bold = new Bold(); parent.Add(bold); ParseElement(e, new SpanTextContainer(parent, bold)); break; case "I": case "EM": var italic = new Italic(); parent.Add(italic); ParseElement(e, new SpanTextContainer(parent, italic)); break; case "U": var underline = new Underline(); parent.Add(underline); ParseElement(e, new SpanTextContainer(parent, underline)); break; case "A": var url = e.Attributes.FirstOrDefault(a => a.NodeName == "href")?.NodeValue; Uri uri; if (url != null && Uri.TryCreate(url.ToString(), UriKind.Absolute, out uri)) { var link = new Hyperlink(); link.NavigateUri = uri; parent.Add(link); ParseElement(e, new SpanTextContainer(parent, link)); } else { ParseElement(e, parent); } break; case "BR": parent.Add(new LineBreak()); break; case "IMG": var src = e.Attributes.FirstOrDefault(a => a.NodeName == "src")?.NodeValue; Uri srcUri; if (src != null && Uri.TryCreate(src.ToString(), UriKind.Absolute, out srcUri)) { var img = new InlineUIContainer { Child = new Windows.UI.Xaml.Controls.Image() { Source = new BitmapImage(srcUri) } }; parent.AddToParentParagraph(img); } break; } } } }
public virtual void RenderElement(IElement element, ITextContainer parent, RenderContextBase context) { var italic = new Italic(); parent.Add(italic); context.RenderNode(element, new SpanContainer(italic)); }
public virtual void RenderElement(IElement element, ITextContainer parent, RenderContextBase context) { parent.Add(new LineBreak()); }
public virtual void RenderElement(IElement element, ITextContainer parent, RenderContextBase context) { parent.Add(new LineBreak()); }
public virtual void RenderElement(IElement element, ITextContainer parent, RenderContextBase context) { var parentContainer = (parent as RichTextBlockContainer).Get(); Span span = new Span(); Underline underline = null; var textDecoration = element.Style("text-decoration"); if (!string.IsNullOrEmpty(textDecoration)) { if (textDecoration == "underline") { underline = new Underline(); underline.Inlines.Add(span); } } var color = element.Style("color"); if (string.IsNullOrEmpty(color) == false) { color = color.Trim().TrimStart('#'); if (color.Length == 6) { var sR = color.Substring(0, 2); var sG = color.Substring(2, 2); var sB = color.Substring(4, 2); byte r, g, b; if (byte.TryParse(sR, NumberStyles.HexNumber, CultureInfo.InvariantCulture, out r) && byte.TryParse(sG, NumberStyles.HexNumber, CultureInfo.InvariantCulture, out g) && byte.TryParse(sB, NumberStyles.HexNumber, CultureInfo.InvariantCulture, out b)) { var value = Color.FromArgb(255, r, g, b); if (value != Colors.White && value != Colors.Black) { if (underline != null) { underline.Foreground = new SolidColorBrush(value); } else { span.Foreground = new SolidColorBrush(value); } } } } } var fontSize = element.Style("font-size"); if (!string.IsNullOrEmpty(fontSize)) { fontSize = fontSize.Replace("px", ""); if (underline != null) { underline.FontSize = double.Parse(fontSize); } else { span.FontSize = double.Parse(fontSize); } } var fontWeight = element.Style("font-weight"); if (!string.IsNullOrEmpty(fontWeight)) { if (underline != null) { underline.FontWeight = FontWeights.Bold; } else { span.FontWeight = FontWeights.Bold; } } var textAlign = element.Style("text-align"); if (!string.IsNullOrEmpty(textAlign)) { if (textAlign == "left") { parentContainer.TextAlignment = Windows.UI.Xaml.TextAlignment.Left; } else if (textAlign == "right") { parentContainer.TextAlignment = Windows.UI.Xaml.TextAlignment.Right; } else { parentContainer.TextAlignment = Windows.UI.Xaml.TextAlignment.Center; } } var textStyle = element.Style("text-style"); if (!string.IsNullOrEmpty(textStyle)) { if (textStyle == "italic") { if (underline != null) { underline.FontStyle = FontStyle.Italic; } else { span.FontStyle = FontStyle.Italic; } } } if (underline != null) { parent.Add(underline); context.RenderNode(element, new SpanContainer(underline)); } else { parent.Add(span); context.RenderNode(element, new SpanContainer(span)); } }
private static void ParseElement(XmlElement element, ITextContainer parent) { foreach (var child in element.ChildNodes) { if (child is Windows.Data.Xml.Dom.XmlText) { if (string.IsNullOrEmpty(child.InnerText) || child.InnerText == "\n") { continue; } parent.Add(new Run { Text = ((XmlText)child).InnerText }); } else if (child is XmlElement) { XmlElement e = (XmlElement)child; switch (e.TagName.ToUpper()) { case "DIV": if (e.GetAttribute("class") == "p") { Paragraph paragraph = new Paragraph(); paragraph.Margin = new Thickness(0, 0, 0, 10); //paragraph.LineHeight = 20; parent.Add(paragraph); ParseElement(e, new ParagraphTextContainer(paragraph)); } else if (e.GetAttribute("class") == "label" && element.GetAttribute("class").Contains("chapter")) { Paragraph chapterLabel = new Paragraph(); chapterLabel.FontFamily = new FontFamily("Segoe UI"); chapterLabel.FontSize = 32; chapterLabel.Inlines.Add(new Run { Text = "Chapter " + e.InnerText }); parent.Add(chapterLabel); } else { ParseElement(e, parent); } break; case "SPAN": //ParseElement(e, parent); if (e.GetAttribute("class") == "heading") { Paragraph header = new Paragraph(); header.FontFamily = new FontFamily("Segoe UI"); header.FontSize = 20; header.Margin = new Thickness(0, 10, 0, 5); header.Inlines.Add(new Run { Text = e.InnerText }); parent.Add(header); } else if (e.GetAttribute("class") == "label" && element.GetAttribute("class").Contains("verse")) { Run run = new Run(); run.Text = e.InnerText; run.FontSize = 8; parent.Add(run); } else { ParseElement(e, parent); } //else //{ // Run run = new Run(); // run.Text = e.InnerText; // parent.Add(run); //} break; case "P": var p = new Paragraph(); parent.Add(p); ParseElement(e, new ParagraphTextContainer(p)); break; case "STRONG": var bold = new Bold(); parent.Add(bold); ParseElement(e, new SpanTextContainer(bold)); break; case "U": var underline = new Underline(); parent.Add(underline); ParseElement(e, new SpanTextContainer(underline)); break; case "A": ParseElement(e, parent); break; case "BR": parent.Add(new LineBreak()); break; } } } }
public virtual void RenderElement(IElement element, ITextContainer parent, RenderContextBase context) { var img = (IHtmlImageElement)element; var source = img.Source; if (string.IsNullOrEmpty(source)) { return; } var image = new Image { Source = new BitmapImage(new Uri(source, UriKind.Absolute)) }; double scale = 1.0; int?width = img.Width(); if (width.HasValue) { if (width.Value > Window.Current.Bounds.Width - 40) { scale = (Window.Current.Bounds.Width - 40) / width.Value; } image.Width = width.Value * scale; } int?height = img.Height(); if (height.HasValue) { image.Height = height.Value * scale; } DisplayInformation.GetForCurrentView().OrientationChanged += (DisplayInformation sender, object args) => { if (sender.CurrentOrientation == DisplayOrientations.Portrait) { double scale1 = 1.0; int?width1 = img.Width(); if (width1.HasValue) { if (width1.Value > Window.Current.Bounds.Width - 40) { scale1 = (Window.Current.Bounds.Width - 40) / width1.Value; } image.Width = width1.Value * scale1; } int?height1 = img.Height(); if (height1.HasValue) { image.Height = height1.Value * scale1; } } else if (sender.CurrentOrientation == DisplayOrientations.Landscape || sender.CurrentOrientation == DisplayOrientations.LandscapeFlipped) { double scale1 = 1.0; int?width1 = img.Width(); if (width1.HasValue) { if (width1.Value > Window.Current.Bounds.Width - 40) { scale1 = (Window.Current.Bounds.Width - 40) / width1.Value; } image.Width = width1.Value * scale1; } int?height1 = img.Height(); if (height1.HasValue) { image.Height = height1.Value * scale1; } } }; parent.Add(image); }
public virtual void RenderElement(IElement element, ITextContainer parent, RenderContextBase context) { var img = (IHtmlImageElement)element; var source = img.Source; if (string.IsNullOrEmpty(source)) { return; } var image = new Image { Source = new BitmapImage(new Uri(source, UriKind.Absolute)) }; double scale = 1.0; int? width = img.Width(); if (width.HasValue) { if (width.Value > Window.Current.Bounds.Width - 40) { scale = (Window.Current.Bounds.Width - 40) / width.Value; } image.Width = width.Value * scale; } int? height = img.Height(); if (height.HasValue) { image.Height = height.Value * scale; } DisplayInformation.GetForCurrentView().OrientationChanged += (DisplayInformation sender, object args) => { if (sender.CurrentOrientation == DisplayOrientations.Portrait) { double scale1 = 1.0; int? width1 = img.Width(); if (width1.HasValue) { if (width1.Value > Window.Current.Bounds.Width - 40) { scale1 = (Window.Current.Bounds.Width - 40) / width1.Value; } image.Width = width1.Value * scale1; } int? height1 = img.Height(); if (height1.HasValue) { image.Height = height1.Value * scale1; } } else if (sender.CurrentOrientation == DisplayOrientations.Landscape || sender.CurrentOrientation == DisplayOrientations.LandscapeFlipped) { double scale1 = 1.0; int? width1 = img.Width(); if (width1.HasValue) { if (width1.Value > Window.Current.Bounds.Width - 40) { scale1 = (Window.Current.Bounds.Width - 40) / width1.Value; } image.Width = width1.Value * scale1; } int? height1 = img.Height(); if (height1.HasValue) { image.Height = height1.Value * scale1; } } }; parent.Add(image); }
public virtual void RenderElement(IElement element, ITextContainer parent, RenderContextBase context) { var bold = new Bold(); parent.Add(bold); context.RenderNode(element, new SpanContainer(bold)); }