在WPF中显示HTML编码样式

时间:2011-06-08 10:44:58

标签: html css wpf html-encode

我从数据库中获取一个包含HTML样式的字符串(不知道它叫什么),我不想在我的WPF应用程序中显示它。

我可以在ViewModel中进行解码,这样我就可以使用.net库了。

我尝试使用WebUtility.HtmlDecode(string)方法,但它没有做任何事情(可能不应该这样做)。

这就是字符串的样子:

<span style="background-color: rgb(255, 102, 0);" class="Apple-style-span">This</span><div><span style="background-color: rgb(255, 102, 0);" class="Apple-style-span">is</span></div><div><span style="background-color: rgb(255, 102, 0);" class="Apple-style-span">a</span></div><div><span style="background-color: rgb(255, 102, 0);" class="Apple-style-span">weird</span></div><div><span style="background-color: rgb(255, 102, 0);" class="Apple-style-span">look</span></div><div></div>

获得我可以用一些XML编码做的文本,但我宁愿使用已经存在的东西。能够获得样式也很棒,但我不打算将WinForms浏览器包括在内。

注意我只需要显示数据,而不是编辑它。

1 个答案:

答案 0 :(得分:2)

这与编码无关,这与转换有关。如果您不想使用WebBrowser控件,则需要将HTML转换为flow content,这可以显示在FlowDocument中。您可以自己编写必要的代码,也可以查看两种方式(或任何其他现有转换器)的this converter是否符合您的需求。


您不一定需要一个合适的HTML页面来让WebBrowser显示它,您可以这样做:

string htmlString = "<span style=\"background-color: rgb(255, 102, 0);\" class=\"Apple-style-span\">This</span><div><span style=\"background-color: rgb(255, 102, 0);\" class=\"Apple-style-span\">is</span></div><div><span style=\"background-color: rgb(255, 102, 0);\" class=\"Apple-style-span\">a</span></div><div><span style=\"background-color: rgb(255, 102, 0);\" class=\"Apple-style-span\">weird</span></div><div><span style=\"background-color: rgb(255, 102, 0);\" class=\"Apple-style-span\">look</span></div><div></div>";
wb.NavigateToString(htmlString);

但理想情况下,你可以先将你的incomming字符串包装在一个合适的html框架中,这应该很容易做到。