C# 在 WinForms 应用程序中呈现 HTML 的最佳方式?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1218325/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 13:48:01  来源:igfitidea点击:

Best Way to Render HTML in WinForms application?

c#html.netwinformswebbrowser-control

提问by Michael Stum

I have a WinForms application, running on .net 3.5. This Application generates HTML on the fly, which includes the complete document, and also an inline-CSS-Stylesheet (inside the head element).

我有一个 WinForms 应用程序,在 .net 3.5 上运行。此应用程序动态生成 HTML,其中包括完整的文档,以及内联 CSS 样式表(在 head 元素内)。

I am using the WebBrowser control and setting browser.DocumentText to my generated HTML, but that does not seem to properly apply styles on the body element (I've set background-color to #000000 in the CSS, but the background is still white).

我正在使用 WebBrowser 控件并将 browser.DocumentText 设置为我生成的 HTML,但这似乎没有正确地在 body 元素上应用样式(我在 CSS 中将 background-color 设置为 #000000,但背景仍然是白色的)。

I wonder if a) there are some alternatives to render relatively simple HTML in C# (i.e. a completely managed HTML renderer) or b) what would be the best way to render HTML using the WebBrowser control, including correct handling of inline-css and without using a temporary file on the hard drive.

我想知道 a) 是否有一些替代方法可以在 C# 中呈现相对简单的 HTML(即完全托管的 HTML 呈现器)或 b) 使用 WebBrowser 控件呈现 HTML 的最佳方式是什么,包括正确处理 inline-css 和不使用硬盘驱动器上的临时文件。

Edit:The CSS-not-applying issue was a separate problem. I've put my actual CSS in a CDATA block, which seems to cause it not to apply correctly. That is now fixed, but the question itself still stands.

编辑:CSS-not-applying 问题是一个单独的问题。我已经把我的实际 CSS 放在一个 CDATA 块中,这似乎导致它不能正确应用。现在已经解决了,但问题本身仍然存在。

采纳答案by Michael Stum

Stayed with the built-in WebBrowser. The HtmlRenderer from the other answer is great, but renders an image.

继续使用内置的 WebBrowser。另一个答案中的 HtmlRenderer 很棒,但会渲染图像。

回答by altso

For managed HTML renderer see HtmlRendereron GitHub.

对于托管 HTML 渲染器,请参阅GitHub 上的HtmlRenderer

回答by Simon

Your could look into using a view engine. Most view engines can work in a stand-alone api manner

您可以考虑使用视图引擎。大多数视图引擎可以以独立的 api 方式工作

for example NHaml and nvelocity both can be used stand alone

例如 NHaml 和 nvelocity 都可以单独使用

http://code.google.com/p/nhaml/

http://code.google.com/p/nhaml/

http://www.castleproject.org/others/nvelocity/index.html

http://www.castleproject.org/others/nvelocity/index.html

And i assume Spark can be as well

我认为 Spark 也可以

http://dev.dejardin.org/

http://dev.dejardin.org/

回答by ChrisW

I have developed what you were asking for: a "completely managed HTML renderer", which will "render relatively simple HTML in C#". It supports a subset of CSS, which you can "specify in a inline CSS stylesheet (inside the head element)".

我开发了您所要求的:“完全托管的 HTML 渲染器”,它将“在 C# 中渲染相对简单的 HTML”。它支持 CSS 的一个子集,您可以“在内联 CSS 样式表中(在 head 元素内)指定它”。

For details, see the ModelText HTML control.

有关详细信息,请参阅ModelText HTML 控件