将简单的HTML转换为RichTextBlock [英] Convert simple HTML to a RichTextBlock

查看:117
本文介绍了将简单的HTML转换为RichTextBlock的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我从Windows 8开始,尝试将HTML转换为 RichTextBlock



I < a href =https://stackoverflow.com/questions/11491134/how-to-show-basic-html-windows8-metro-style-textblock>已阅读,我可以使用此功能: HtmlUtilities.ConvertToText 在一个 TextBlock 中,但是我找不到在 RichTextBlock



从我的理解和尝试我无法扩展 RichTextBlock ,所以我无法应用此功能每次调用一个 RichTextBlock



此外,我找不到任何方法将文本绑定到 RichTextBlock 建立一个解析器只是为了简单的HTML(我只想要段落和斜体/粗体)似乎是一种矫枉过正。此外,我不知道应该在哪里解析,因为 RichTextBlock 似乎无法使用。



我可以' t使用 WebView ,因为我需要透明度(并且从我读过的 WebView没有它)。

编辑

@mydogisbox让我看到我对自己的感觉太过分了研究。

我可以使用 HtmlUtilities.ConvertToText 来获取属性的getter,我可以在 RichTextBlock 。我无法绑定它,因为我试图做
< Run Text ={Binding TextHTML}/> ,而没有<段落> 标记。


然而 HtmlUtilities.ConvertToText 不会保留斜体或粗体。只有段落:/。

我最终使用了 package在gitHub上可用,可以将HTML从HTML转换为RickTextBlock。



Basiclly你只需要打开程序包管理器控制台(工具>程序包程序包管理器>程序包管理器控制台)和安装程序包运行 Install-Package RichTextBlock.Html2Xaml

然后你打开RichTextBlockProperties.cs,你有你需要复制的行。在我的情况下,我不得不添加命名空间:

  xmlns:rtbx =using:EventTests.Common

然后,您可以使用HTML绑定您的属性:

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

一些问题和一些解决方案



我在这个库中发现的一个问题是它如何处理没有div的简单html。像:

 < p>测试< i>斜体< / i>等等。< / p> 
< p>更多测试< / p>

打印:


测试斜体等等。

更多测试

然而,我想要这样的东西:


测试 italic 等等。



更多测试

所以我不得不将第二段换成div(除了第一段以外的所有段落都可以被换行)。

 < p>测试< i>斜体< / i>等等。< / p> 
< div>< p>更多测试< / p>< / div>

如果您换行第一段,那么您将会有一个额外的新行。



到目前为止,这是我找到的最佳解决方案。如果你觉得更好,我会在测试和学习之后加以补充。如果你找到更好的解决方案,我会接受你的。



请小心



如果您有像<的符号,这种方法会崩溃或&在你的HTML。我建议你在尝试使用这个库之前替换这些字符。


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

I have read that I could use this function : HtmlUtilities.ConvertToText in a TextBlock but I can't find a way to use this function in a RichTextBlock!

From what I understand and tried I can't extend the RichTextBlock so I can't apply this function everytime a RichTextBlock is called.

Also, I can't find any way to bind text to a RichTextBlock and building a parser just for simple HTML (I only want paragraphs and italics/bolds) seems an overkill. Also, I have no idea where I should do this parsing since I the RichTextBlock seems unextendable.

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

EDIT

@mydogisbox made me see I was getting too far on my research.

I can use HtmlUtilities.ConvertToText in the getter of a property that I can bind in the RichTextBlock. I couldn't bind it because I was trying to do <Run Text="{Binding TextHTML}" /> without a <Paragraph> tag.

However HtmlUtilities.ConvertToText doesn't preserve italics or bolds. Only paragraphs :/.

解决方案

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.

这篇关于将简单的HTML转换为RichTextBlock的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆