uma raja
uma raja

Reputation: 1113

How to show HTML content in TextBlock using XAML from UWP app?

In JSON response:

results =  "<p>This is a <b>paragraph</b></p><p>New paragraph with symbols &gt; tags</p>";

XAML:

<Textblock  Text={Binding results}/>

result:

This is a **paragraph** New Word
New paragraph with symbols > tags

Upvotes: 0

Views: 720

Answers (1)

Martin Zikmund
Martin Zikmund

Reputation: 39072

You can use RichTextBlock to more easily match HTML DOM with XAML output. Unfortunately, there is not built-in API that will transform HTML into equivalent XAML for the control.

You can parse the HTML into known tags using HtmlAgilityPack and add items into RichTextBlock.Inlines manually. There is an old Docs article on this process, but it still applies. One of the examples it shows:

private static Inline GenerateBlockForNode(HtmlNode node)
{
    switch (node.Name)
    {
        case "div":
            return GenerateSpan(node);
        case "p":
        case "P":
            return GenerateInnerParagraph(node);
        case "img":
        case "IMG":
            return GenerateImage(node);
        ...

The individual GenerateXXX methods then generate appropriate inlines:

private static Inline GenerateSpan(HtmlNode node)
{
    Span s = new Span();
    AddChildren(s, node);
    return s;
}

The easiest solution would be to use the code in this GitHub repo, which implements a lot the tag conversion and maybe you will be able to just copy-paste the converter to your project and get running.

Upvotes: 1

Related Questions