Building “rich desktop clients” is not a very easy task. WPF can be a good choice for “fat” Windows Apps, but if you have zero knowledge in XAML you might end up in hell, because… XAML, right? If you are more a “web guy” (like myself) or just want to reuse existing code from your Web-App and want to stick to your .NET know-how this blogpost might come handy.
So, back to .NET land and the idea is clever and kinda stupid together: We want to build a small app that embeds a browser and a server (this is more or less the same trick from Electron & co.). The host should be a WPF application, because with this we could combine interesting frameworks in one application - e.g. legacy code stuff, WPF controls, web stuff… yeah!
If you just want to display “Web-Content” you can just use the built-in WebBrowser-Control. The most common and annoying problem is that the default rendering engine is stuck in the stone age: Internet Explorer 7. Even on Windows 10 the default rendering engine will be set to IE7. To fix this you can use some Registry-Magic, but this is not very elegant.
A better solution would be to take a deeper look at the Chromium Embedded Framework for .NET. It is the rendering engine of Chrome - which gives you a super powerful platform - and is super easy to use. The only downside: You need to compile your app for x86 or x64 - AnyCPU won’t work.
So… let’s take a look how we can achieve our goal.
A good option is using the HttpListener with the ASP.NET WebApi - of course you could also use NancyFx or any other OWIN/”self-host” web framework. The hosting application will use the HttpListener from your System and will listen to a specific port. As long as you use a high port number you don’t need admin privileges.
For the demo I embedded the HTML inside the application, but this could be read from a resource file or any other storage in real life.
As far as I know maybe you could also do some other hosting tricks, so that the application doesn’t really needs to listen to a specific port, e.g. hosting the stuff in-memory.
// parameter in the WebApp.Start method.
The controller code: Simple ApiController which reads a embedded HTML file.
Ok… as I told you this blogpost shows you a really stupid or really clever trick (it depends on your habbits… WebGuy vs. WindowsGuy). In this example we used a self-hosting WebApi to display an embedded HTML page via CEF Sharp inside a Windows App.
The full code can be found on GitHub.