Convert plain HTML to RichTextBlock

I am starting with Windows 8 and I am trying to convert HTML to RichTextBlock .

I read that I could use this function: HtmlUtilities.ConvertToText in TextBlock , but I can not find a way to use this function in RichTextBlock

From what I understand and tried, I cannot extend RichTextBlock , so I cannot apply this function every time RichTextBlock .

Also, I can't find a way to bind text to RichTextBlock and create a parser just for plain HTML (I only need paragraphs and italics / bold) seems redundant. Also, I have no idea where I should do this parsing, since I RichTextBlock seems to be irreversible.

I can’t use WebView because I need transparency (and from what I read, WebView doesn't have it ).

EDIT

@mydogisbox made me see that I was too drawn in in my studies.

I can use HtmlUtilities.ConvertToText in a getter property that I can bind in a RichTextBlock . I could not bind it because I tried to do this <Run Text="{Binding TextHTML}" /> without the <Paragraph> .

However, HtmlUtilities.ConvertToText does not save italics or bold. Only paragraphs: /.

+7
source share
1 answer

I ended up using the package available for gitHub , which converts from HTML to RickTextBlock.

Basically, you need to open the package manager console (Tools> Library Package Manager> Package Manager Console) and install the package under Install-Package RichTextBlock.Html2Xaml .

Then you open RichTextBlockProperties.cs and you have the lines you need to copy. In my case, I had to add a namespace:

 xmlns:rtbx="using:EventTests.Common" 

And then you can associate your property with HTML using:

 <RichTextBlock rtbx:Properties.Html="{Binding ...}"/> 

Some problems and some solutions.

The problem I found in this library is how it handles plain html without a div. How:

 <p>Testing <i>italic</i> and something more.</p> <p>More testing </p> 

Fingerprints:

Italics testing and more.
More testing

However, I wanted something like this:

Italics testing and more.

Additional tests

So I had to wrap the second paragraph in a div (and all paragraphs except the first can be wrapped).

 <p>Testing <i>italic</i> and something more.</p> <div><p>More testing </p></div> 

If you complete the first paragraph, you will have a new line.

So far this is the best solution I have found. If you find that I rate it better, as I test and study. If you find a better solution, I will accept yours.

be careful

This approach will work if you have characters like "<" or "&" in your html. I suggest you replace these characters before trying to use this library.

+3
source

All Articles