输出html的WPF应用程序

发布于 2024-09-13 12:51:14 字数 1082 浏览 8 评论 0原文

我正在创建一个 WPF(在 c# 中)应用程序,它将根据其输出创建 html 文件。有没有一种方法可以在桌面应用程序中使用 html“视图”(来自 asp.net mvc)的概念,而无需重新发明轮子?基本上我只想保存 html 中的输出项目列表,这在 Web 应用程序中非常简单(甚至在 wpf 桌面应用程序中使用绑定来在窗口中显示输出),但我觉得桌面应用程序中的 HTML 输出似乎很笨重就像我要重新发明轮子一样,在我的数据输出程序中包含灵活的 html 模板。

当然有一种快速而优雅的方法可以做到这一点吗?

举个例子,程序的输出是一个名称列表(Bill、Bob、Jill、Frank),我想将以下 HTML 文件保存到磁盘:

<html>
<body>
<ul>
<li>Bill</li>
<li>Bob</li>
<li>Jill</li>
<li>Frank</li>
</ul>
</body>
</html>

如果用户定义了一个 html 模板,例如

<html>
<body>
<ul>
<li><!-- user name --></li>
</ul>
</body>
</html>

:如果用户定义了类似于以下内容的内容,则可以在 ASP.NET MVC 中完成(或在 PHP、Ruby 等中以非常类似的方式完成):

<html>
<body>
<ul>
    <% foreach (string name in NameList) { %>
        <li>
        <%: name %>
        </li>
    <% } %>
</ul>
</body>
</html>

并且我想知道是否有一种简单的方法可以使用以下命令创建 html 文件桌面应用程序中的该方法?

I'm creating a WPF (in c#) application that will create html files based on it's output. Is there a way to use the concept of an html 'view' (from the asp.net mvc) in a desktop application without reinventing the wheel? Basically I just want to save a list of outputted items in html which is so simple in a web application (and even using binding in a wpf desktop application to show the output in window) but seems clunky for HTML output in a desktop application I feel like I'd be reinventing the wheel to include flexible html templates with my program for data output.

Surely there is a quick and elegant way to do this?

As an example say the output of the program were a list of names (Bill, Bob, Jill, Frank), I'd like to save the following HTML file to the disk:

<html>
<body>
<ul>
<li>Bill</li>
<li>Bob</li>
<li>Jill</li>
<li>Frank</li>
</ul>
</body>
</html>

if the user defined a an html template such as:

<html>
<body>
<ul>
<li><!-- user name --></li>
</ul>
</body>
</html>

which could be done in ASP.NET MVC (or in a very similar way in PHP, ruby, etc) if the user defined something similar to the following:

<html>
<body>
<ul>
    <% foreach (string name in NameList) { %>
        <li>
        <%: name %>
        </li>
    <% } %>
</ul>
</body>
</html>

and I'd like to know if there is an easy way to create html files using that method in a desktop application?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

匿名。 2024-09-20 12:51:14

以易于序列化为 XML 的形式存储数据 - 无论是在实现 XML 序列化的类中,还是在 ADO.NET DataSet 中。编写 XSLT 模板以将 XML 转换为 XHTML。使用 XslCompiledTemplate 类实现转换。

诚然,这种方法需要您学习一些您可能不太了解的东西。 XSLT 并非无足轻重,XML、XSLT 和 HTML 中的微妙之处有时会让您感到困惑。但一旦你学会了它,它就是一个令人难以置信的强大技术组合。我已经有十年没有手工制作过 HTML 文档了;对于必须生成的任何 HTML 文档,我的出发点都是一个 XML 文档,其中包含其内容和用于呈现内容的 XSLT 转换。

从 WPF 方面来看,您需要处理的唯一问题是在应用程序中显示呈现的 HTML。我用两种方法做到了这一点。一种方法是将 WinForms WebBrowser 控件包装在公开 Html 依赖属性的 UserControl 中,以便我可以绑定视图模型的 Html 属性。 (视图模型都共享一个帮助器类,用于获取 XML 并将其转换为 HTML。)

将输出获取到 WPF UI 的另一种更困难的方法是完全绕过 HTML 并编写一个 XSLT 转换,将数据呈现为 FlowDocument。它本身实际上并不难,它只是引入了一系列您必须考虑的新问题 - 而且,如果 HTML 生成确实是它的全部内容,那么它可能完全不适合您的应用程序。但是,一旦您知道如何将 XML 数据转换为 HTML,将其转换为 XAML 就变得轻而易举了。

Store your data in a form that is readily serialized to XML - either in a class that implements XML serialization, or an ADO.NET DataSet. Write XSLT templates for transforming the XML into XHTML. Implement the transformations using the XslCompiledTemplate class.

Granted, that approach will require you to learn some things that you probably don't know very much about. XSLT's not trivial, and there are subtleties in XML, XSLT, and HTML that will trip you up from time to time. But once you learn it, it's an incredibly powerful combination of technologies. I haven't produced an HTML document by hand in a decade; my starting point for any HTML document that I have to produce is an XML document that contains its content and an XSLT transform for rendering the content.

From the WPF side, the only issue that you'll need to deal with is in displaying rendered HTML in your application. I've done this two ways. One is by wrapping the WinForms WebBrowser control in a UserControl that exposes an Html dependency property, so that I can bind my view models' Html property to it. (The view models all share a helper class for getting their XML and transforming it to HTML.)

Another, harder way to get the output into a WPF UI is to bypass HTML completely and write an XSLT transform that renders the data as a FlowDocument in XAML. It's not actually harder per se, it just introduces a bunch of new issues that you have to think about - also, it may be totally inappropriate for your application, if HTML production is truly what it's all about. But once you know how to transform XML data into HTML, transforming it into XAML is child's play.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文