WPF: displaying HTML-based content stored in a resource assembly

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

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

I can intercept navigation requests and serve HTML content using the navigation event and the NavigateToString () method. But this does not work to intercept calls to download images, CSS files, etc.

In addition, I know that HTML is in the application example FlowDocument SDK , which may be useful, but I will probably have to significantly expand the selection for image processing and style sheets.

For what it's worth, we also create HTML content that needs to be rendered (via Wiki pages), so the original HTML is somewhat predictable (for example, there can be no JavaScript) in terms of image link locations and CSS styles used. We want to display random HTML content from the Internet.

Update: There is also the possibility of creating an MHT file for each HTML page, which would "embed" all images in the form of MIME types and reduce the need for finer callbacks.

+7
html mhtml controls wpf
source share
2 answers

If you are using the 28 megabyte DLL version, you can take a look at BerkeliumSharp , which is the managed shell around the amazing Berkelium library. Berkelium uses the chromium browser at its core to provide off-screen rendering and a delegated event model. There are many really cool things you can do with this, but for your specific problem, Berkelium has an interface called ProtocolHandler. The purpose of the protocol handler is to get the URL and provide the HTTP headers and body with the basic rendering engine.

In the BerkeliumSharp test application (one of the projects available in the source), you can see that one of them is FileProtocolHandler - it processes the entire IO file for the "file: //" protocol using the .NET Managed Classes (System.IO). You can do the same for a compiled protocol, such as "resource: //". There really is only one method that you must override, called HandleRequest, which looks like this:

 bool HandleRequest (string url, ref byte[] responseBody, ref string[] responseHeaders) 

Therefore, you should use a URL such as "resource: // path / to / my / html" and do all this in this Assembly.GetResourceStream method, etc. It should be pretty easy to take a look at how FileProtocolHandler is used to adapt your own.

Both berkelium and berkelium sharp are open source with a BSD license.
+3
source share

WebBrowser provides a NavigateToStream (stream) method that may work for you:

If your content is then saved as an embedded resource, you can use:

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

There is also the NavigateToString(string) method, which expects the contents of the string document.

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

+3
source share

All Articles