WPF:显示存储在资源程序集中的基于HTML的内容 [英] WPF: Display HTML-based content stored in resource assembly

查看:121
本文介绍了WPF:显示存储在资源程序集中的基于HTML的内容的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在WPF项目中,我需要呈现基于HTML的内容,该内容存储在WPF项目引用的资源程序集中。

In my WPF project I need to render HTML-based content, where the content is stored in a resource assembly referenced by my WPF project.

WPF框架和WebBrowser控件。不幸的是,它们都只公开了Navigation事件(Navigating,Navigated),但没有任何事件允许我根据请求的URL返回从资源程序集中检索的HTML内容。

I have looked at the WPF Frame and WebBrowser controls. Unfortunately, they both only expose Navigation events (Navigating, Navigated), but not any events that would allow me, based on the requested URL, to return HTML content retrieved from the resource assembly.

我可以使用Navigating事件和NavigateToString()方法拦截导航请求并提供HTML内容。但这对拦截图像,CSS文件等的加载调用无效。

I can intercept navigation requests and serve up HTML content using the Navigating event and the NavigateToString() method. But that doesn't work for intercepting load calls for images, CSS files, etc.

此外,我知道 HTML到Flowdocument SDK示例应用程序可能很有用,但我可能不得不大大扩展该示例处理图像和样式表。

Furthermore, I am aware of an HTML to Flowdocument SDK sample application that might be useful, but I would probably have to extend the sample considerably to deal with images and style sheets.

对于它的价值,我们还生成了要呈现的HTML内容(通过Wiki页面),因此源HTML有点可预测(例如,可能没有JavaScript)来表示参考图片位置和所用CSS样式表。我们希望在互联网上显示随机的HTML内容。

For what it is worth, we also generate the HTML content to be rendered (via Wiki pages) so the source HTML is somewhat predictable (e.g., maybe no JavaScript) in terms for referenced image locations and CSS style sheets used. We are looking to display random HTML content from the internet.

更新:
也可以创建MHT文件对于每个HTML页面,它将内联所有图像作为MIME类型,并减少了具有更细粒度的回调的需要。

Update: There is also the possibility to create an MHT file for each HTML page, which would 'inline' all images as MIME-types and alleviate the need to have finer-grained callbacks.

推荐答案

WebBrowser 公开了 NavigateToStream(Stream)方法可能对您有用:

The WebBrowser exposes a NavigateToStream(Stream) method that might work for you:

如果随后将内容存储为嵌入式资源,则您可以使用:

If your content is then stored as an embedded resource, you could use:

var browser = new WebBrowser();
var source = Assembly.Load("ResourceAssemblyName");
browser.NavigateTo(source.GetManifestResourceStream("ResourceNamespace.ResourceName"));

还有一个 NavigateToString(string)期望文档内容为 string 的方法。

There is also a NavigateToString(string) method that expects the string content of the document.

注意:我从没在愤怒中使用过,所以我不知道会有多少帮助!

Note: I have never used this in anger, so I have no idea how much help it will be!

这篇关于WPF:显示存储在资源程序集中的基于HTML的内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆