Recursive AI

Use a C# WebAssembly component as a custom element

OneApp is a free, open-source framework for desktop apps in .NET whose UI is HTML and CSS, drawn natively with no web view, from Recursive AI. MIT or Apache-2.0, no native DLLs to ship.

OneApp also runs OneUI components in the browser as HTML custom elements. OneUI is a .NET library for building UI components from HTML, CSS, and C#.

Terminal
dotnet workload install wasm-tools
dotnet add package RecursiveAI.OneApp.Browser
dotnet add package RecursiveAI.OneUI

Requires .NET 10 (net10.0); .NET 9 and earlier aren’t supported.

C#
using OneApp.Browser;
 
BrowserApp.Define("demo-greeting",
  _ => new Greeting(),
  o => o.StringParam("name",
    (view, name) => view.Name = name));

Greeting is the component defined in Greeting.html and Greeting.cs below.

The example is the full program below: unzip it and dotnet run.

Open the HTTP App url printed by dotnet run in your browser. The page displays:

Text
Hello, Ada!

Full program

The zip’s GreetingDemo.csproj uses Microsoft.NET.Sdk.WebAssembly instead of the console SDK. This project copies the bridge module from the NuGet package into wwwroot so the development server can serve it:

XML
<Project Sdk="Microsoft.NET.Sdk.WebAssembly">
  <PropertyGroup>
    <OutputType>Exe</OutputType>
    <TargetFramework>net10.0</TargetFramework>
    <ImplicitUsings>enable</ImplicitUsings>
    <WasmFingerprintAssets>false</WasmFingerprintAssets>
    <InvariantGlobalization>true</InvariantGlobalization>
    <AllowUnsafeBlocks>true</AllowUnsafeBlocks>
    <DefaultItemExcludes>$(DefaultItemExcludes);wwwroot/oneui-browser.js</DefaultItemExcludes>
  </PropertyGroup>
  <ItemGroup>
    <PackageReference Include="RecursiveAI.OneApp.Browser" Version="1.0.0" GeneratePathProperty="true" ExcludeAssets="build" />
    <PackageReference Include="RecursiveAI.OneUI" Version="1.0.0" />
    <Content Include="wwwroot/oneui-browser.js" />
  </ItemGroup>
  <Target Name="CopyOneAppBrowserModule" BeforeTargets="PrepareForBuild">
    <Copy SourceFiles="$(PkgRecursiveAI_OneApp_Browser)/content/oneui-browser.js"
          DestinationFolder="wwwroot" SkipUnchangedFiles="true" />
  </Target>
</Project>

Program.cs registers the tag. The assembly annotation tells the analyzer that this program targets the browser:

C#
using OneApp.Browser;
using System.Runtime.Versioning;
 
[assembly: SupportedOSPlatform("browser")]
 
BrowserApp.Define("demo-greeting", _ => new Greeting(),
    options => options.StringParam("name", (view, name) =>
        view.Name = name));

Greeting.html is the component template. OneUI’s compiler generates the other half of the partial class and the implementation of its [State] property:

HTML
<p>Hello, {Name}!</p>

Greeting.cs:

C#
using OneUI.Components;
 
public partial class Greeting
{
    [State] public partial string Name { get; set; }
}

wwwroot/index.html supplies the name input and starts the runtime:

HTML
<!doctype html>
<meta charset="utf-8">
<title>C# greeting</title>
<demo-greeting name="Ada"></demo-greeting>
<script type="module">
  import { boot } from './oneui-browser.js';
  await boot();
</script>

If the greeting stays blank, check that oneui-browser.js and _framework/dotnet.js load over HTTP; opening index.html as a local file isn’t the run path.

Variations

Pass a different name from JavaScript. Run these lines in the browser console once the page shows Hello, Ada!:

JAVASCRIPT
const greeting = document.querySelector('demo-greeting');
greeting.name = 'Lin';
Text
Hello, Lin!

The short answer

Use BrowserApp.Define to register a tag and tell OneApp how to construct its C# component. Use StringParam to turn an HTML attribute into a value the component can render. Then place the tag in index.html and call boot() to load the .NET WebAssembly runtime and run Main.

How do you create a custom element in HTML?

Pick a lowercase name with a hyphen, such as demo-greeting. Writing the tag in HTML doesn’t register it; BrowserApp.Define calls the browser’s customElements.define when the runtime starts. If the tag is already in the document, it upgrades at registration. OneApp creates a component instance for each element, so two greetings can have different names on the same page.

Use an attribute for the initial name in HTML. OneApp observes that attribute and exposes a matching JavaScript property, so updates from either side reach the same C# setter. A dashed attribute maps to a camel-case property: due-date becomes dueDate. Removing an attribute mapped by StringParam passes an empty string to the component. The sample has an open shadow root by default: inspect the rendered paragraph inside it in DevTools, not as a child of the host in the main document tree.

Can C# run in a browser without a .NET web server?

Yes. The C# component runs on browser-wasm. The JavaScript bridge applies its updates to the browser DOM and forwards DOM events to .NET. The runtime still has to be served over HTTP, but the web server only has to serve the published files. Run dotnet publish -c Release and serve bin/Release/net10.0/publish/wwwroot/ as a unit. Keep _framework/ beside the bridge module; boot() loads the runtime from there by default. See the OneApp browser reference for lifecycle hooks and events sent back to JavaScript.

All oneapp recipes