2011-06-08 96 views
1

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

我可以在ViewModel中解码,因此我可以使用.net库。

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

这是字符串可能是什么样子:

<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浏览器。

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

回答

2

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


你不一定需要一个适当的HTML页面,使web浏览器显示出来,你可以这样做:

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); 

但是最好你可以在适当的包装你进来的字符串首先是html-frame,这应该很容易做到。

+0

所以有一个WebBrowser控件?我只是觉得它只存在于Forms中。我会检查出来的。 – 2011-06-08 15:07:20

+0

[There is。](http://msdn.microsoft.com/zh-cn/library/system.windows.controls.webbrowser.aspx) – 2011-06-08 15:40:30

+0

H.B.不过我不确定这可能会有多大用处。我只有这个字符串,而不是一个指向它的网站。我怎么能得到它来呈现html字符串? – 2011-06-08 16:12:07