Convert simple HTML to a RichTextBlock

青春壹個敷衍的年華 提交于 2019-11-30 14:05:42

I ended up using a package avaiable on gitHub that converts from HTML to a RickTextBlock.

Basiclly you only need to open the Package Manager Console (Tools > Library Package Manager > Package Manager Console) and install the package running 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 the namespace:

xmlns:rtbx="using:EventTests.Common"

And then you can bind your property that has HTML using:

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

Some problems and some solutions

A problem I have found with this library is how it handles simple html with no divs. Like:

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

This prints:

Testing italic and something more.
More testing

However, I wanted something like this:

Testing italic and something more.

More testing

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

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

If you wrap the first paragraph then you will have an extra new line.

So far this is the best solution I have found. If you find better I apreciate it since I am testing and learning. If you find a better solution I will accept yours.

Be carefull

This approach will crash if you have symbols like "<" or "&" in your html. I suggest that you replace those chars before you try to use this library.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!