wpf 在 XAML + windows 8 中渲染 Html
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12730813/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Render Html in XAML + windows 8
提问by user1428798
I have a proprety in my model that contain HTML ex:
我的模型中有一个包含 HTML ex 的属性:
public class watch
{
public string description=<div>hello<br/> world</div>
}
I need in the view in xaml code to bind this description with a composant.
which composant should i use (RichTextblock or ...) to render this html??
if i use <textblock text={binding="descrption"}/>the html tags still visible, i need to return to ligne and do not show html tags
我需要在 xaml 代码的视图中将此描述与组合程序绑定。我应该使用哪个组合器(RichTextblock 或 ...)来呈现这个 html?如果我使用<textblock text={binding="descrption"}/>仍然可见的 html 标签,我需要返回到 ligne 并且不显示 html 标签
Any ideas please??
请问有什么想法吗??
Best regards
此致
回答by manojlds
The WebView control gives us a way to host HTML data within our app. But if we look at its Source property, we see that it takes the Uri of the web page to display. Our HTML data is just a string of HTML. It doesn't have a Uri that we can bind to the Source property. Luckily, there is a NavigateToString method that we can pass our string of HTML to.
WebView 控件为我们提供了一种在应用程序中托管 HTML 数据的方法。但是如果我们查看它的 Source 属性,我们会发现它需要网页的 Uri 来显示。我们的 HTML 数据只是一串 HTML。它没有可以绑定到 Source 属性的 Uri。幸运的是,我们可以将 HTML 字符串传递给一个 NavigateToString 方法。
So do:
所以这样做:
ContentView.NavigateToString(w.description);
http://msdn.microsoft.com/en-us/library/windows/apps/br211380.aspx
http://msdn.microsoft.com/en-us/library/windows/apps/br211380.aspx
回答by Sean
回答by xleon
There′s another approach, that is building the parts (blocks) of a XAML RichTextBlockfrom an Html text. It involves more work than a WebView and it′s hard to support all tags, but if you just need a limited subset of html tags and you want more formatting control it′s very handy. Take a look at this repo
还有另一种方法,即RichTextBlock从 Html 文本构建 XAML 的部分(块)。它比 WebView 涉及更多的工作,并且很难支持所有标签,但是如果您只需要有限的 html 标签子集并且您想要更多的格式控制,它非常方便。看看这个 repo
You basically bind your description property:
您基本上绑定了您的 description 属性:
<RichTextBlock html:Properties.Html="{Binding description}"/>
Here′s an example:
这是一个例子:


