Sample project to illustrate how Blazor can be connected to a custom target allowing DOM operations to be handled directly within C# - instead of going via some interop layer that translated the DOM operations into JavaScript. The result is a cross-platform application that uses Blazor as its framework with rendering fully done in managed code.
Disclaimer: We might argue about the word "native" here - what I mean by it is "a C# solution that does not rely on JavaScript or external helpers and can render the same way across different platforms".
The example is a simple Blazor application contained in BlazorExample.Components. It contains images, some interactivity, and some state handling. It is styled globally via plain CSS.
When opened the application looks like this (Windows, other OS would work, too):
The theme can be configured in the settings page:
There is not much to say here except that everything you see rendered is fully done in C# - no browser (or browser control) has been used in this example. The application itself uses Avalonia to be fully cross-platform.
Let's say you have assemblies:
- An application using Avalonia (cross-platform UI)
- A set of Blazor components / a Blazor application (important: not an ASP.NET Core server using Blazor or similar, but something as pure as an RCL, a Razor Component Library)
What we want to achieve here is to render the contents of (2), i.e., a Blazor app, inside your native UI (1). Luckily, we already build everything and combined it in AngleSharp.Browser. AngleSharp.Browser gives us - among other things - an Avalonia component that holds a rendering process underneath. The whole rendering is done by AngleSharp (HTML loading and parsing, CSS evaluation, rendering, as well as web compatible APIs).
Giving those prerequisites we can create an architecture flow like this:
Blazor Components (ours)
|
v
Blazor Renderer (Microsoft)
|
v
CustomDomRenderer (ours/AngleSharp.Blazor)
|
v
C# DOM (AngleSharp)
|
v
Rasterization Engine (AngleSharp.Renderer)
|
v
Avalonia Control (AngleSharp.Browser.Avalonia)
In short, we follow this diagram here:
Essentially we connect Blazor's renderer to the DOM as constructed from AngleSharp. Blazor's renderer already allows this as Blazor can do various integrations, e.g., via JavaScript/WASM to an embedded WebAssembly module or via JavaScript/WebSocket to an ASP.NET Core server. Now the renderer uses C#/AngleSharp to the code running in the same app domain.
How is this different from Blazor Hybrid, i.e., a native application (e.g., using MAUI) with a webview?
It is similar in the sense that in both cases the Blazor part is directly in the executing application, however, while Blazor Hybrid uses a webview (i.e., external browser component displayed in place) with its own rendering thread and resource management, this sample has everything in the same app domain.
Consequences of this design: There is no JavaScript necessary. True, in Blazor Hybrid you don't need to write JavaScript, however, as the external browser component does not expose the DOM directly for manipulation in C#, Blazor needs to couple to it via a JavaScript interop script. This script performs the rendering tasks from the Blazor Hybrid renderer. In contrast, we have the renderer here attached directly to the DOM. As a result, interactions can be much more direct and we can be much more resource saving.
Another benefit of this architecture is that we can essentially customize everything. As an example we can add a custom URL handler which makes it possible to have the application served from an address such as app:/index.html such that an image like foo.png is resolved from app:/foo.png, which will use our custom resolver. This way, there is no restriction what resources can be used and applications formerly created for the web could just run.
I have no illusion that this has the same quality as an evergreen browser such as Firefox or packaging this into an Electron app with a pre-determined Chromium version.
The limitations that I see are partially in the actual rendering engine (AngleSharp.Browser / AngleSharp.Renderer):
- CSS coverage: AngleSharp.Renderer is not a full browser engine yet. Every larger app might hit CSS where it might get problems (though these are all bugs we want to fix if we encounter them): complex grid, transitions and animations, position: sticky edge cases, fonts, form-control sizing. Component libraries built for real browsers such as MudBlazor or Fluent UI might probably look broken in places.
- Performance: each DOM mutation may trigger a new layout, which - at the moment - is fairly expensive. Therefore, the saved cost of a larger embedded browser such as Chromium with the JavaScript bridge is more than nullified, unfortunately. However, we want to improve here and we already started with an incremental path for rendering. Still, large DOMs (such as big tables or long lists) can mean slow frames. Today, Layout and painting run on the same event loop as the components, so a slow frame also delays event handling.
- Accessibility: the window is a painted bitmap. Screen readers and OS accessibility APIs see nothing; there is no accessibility tree.
- Text input: input method editors (IME), spell checking, rich text editing, and native text selection and context menus are limited compared to a real browser.
The Blazor integration (presented in BlazorExample.Core):
- No JavaScript: a large part of the Blazor ecosystem depends on JS interop:
IJSObjectReferencemodules (import), libraries that wrap JS (charts, editors, maps),ElementReferencemeasurements,InputFile, clipboard and storage APIs. The C#IJSRuntimeonly implements a few built-in identifiers. Everything else needs a hand-written .NET replacement or fails with aJSException. - Event model gaps:
- Only the events the engine actually fires reach components. Focus/blur, pointer capture, drag and drop, touch and wheel details are partial or missing.
- Text inputs only fire input, not change on blur. So the default
@bindon text inputs, andEditForm/InputTextvalidation, behave differently from a browser; the sample uses @oninput to avoid this. - Custom event types (
[EventHandler]registrations) aren't supported, and multi-select<select>handling is minimal.
- Missing framework features:
- Virtualize, which relies on JS for scroll measurement.
- Scoped CSS (
.razor.cssbundles aren't served). - Static web assets of other Razor class libraries: only the components' embedded
wwwrootis served, not_content/{Library}/...paths. PersistentComponentState, prerendering, render modes, enhanced navigation and streaming rendering.
- Navigation: it has its own simple back stack and doesn't use the engine's history. There's no forward navigation and no hash-only navigation semantics.
- Edit application is a re-implementation: applying Blazor's changes to the DOM ports Blazor's JS renderer from scratch. Blazor's own special cases (select values, SVG namespaces,
@keypermutations, markup blocks) have only been exercised by this small sample. Subtle diffing edge cases in a large app could leave the DOM out of sync with the components. And the edit types are internal-ish API (the analyzer reserves them for the framework, warningBL0006), so a future .NET version could change them. - Threading: every component continuation is posted through
BrowserEngine.InvokeAsync, which waits for the engine's lock. Many timers or async continuations mean heavy contention with frame preparation, and a blocking call inside a component stalls the whole UI, including painting. Code that calls.Result/.Wait()on the event loop can deadlock. - Error handling: unhandled component exceptions are only logged or raised as an event. There's no error UI like Blazor's
#blazor-error-ui, and no reconnect or recovery story.
Some more limitations due to the hosting and tooling parts:
- No developer tools: no browser devtools and no hot reload (yet - though both are possible to achieve and we have an inspector already). Right now, debugging layout problems means guessing what AngleSharp.Renderer did.
- Embedded assets only: changing CSS or images means rebuilding the components assembly.
StaticAssets.AddDirectoryexists but isn't used.
Right now, the shown architecture fits well for smallish, self-contained UIs written with this host in mind: tools, kiosks, embedded dashboards with controlled CSS and no third-party JS-based component libraries (even though using JS with AngleSharp.Js would work, too). For larger Blazor applications it would be better to choose Blazor Hybrid (a real web view).
Consequently, if there is enough interest we'd invest to make rendering faster and more incremental. In addition, we'd package this up nicely, such that all you'd need to do is create a new Avalonia app and put a component in.
Get in touch if you want to collaborate or are interested in seeing this evolve.
This code is released using the MIT license. For more information see the LICENSE file.


