Recursive AI

Use a C# UI component from another assembly

OneUI is a free, open-source .NET library from Recursive AI for writing UI components in HTML, CSS, and C# that run in native windows on desktops and phones, or in a browser via WebAssembly, with OneApp. MIT or Apache-2.0, no native dependencies.

Add a project reference to the class library (Widgets here). Use the component’s class name as a tag in your app’s template, Checkout.html.

Terminal
dotnet add package RecursiveAI.OneUI

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

Checkout.html:

HTML
<PriceBadge Dollars="25" />

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

Prints <span>Price: $25</span> from Widgets.dll.

The full program

The download includes both projects. To set them up by hand, use the .NET 10 SDK. In an empty folder, run dotnet new console and the install command above to create the app. From the same folder, create the class library and install OneUI in it:

Terminal
dotnet new classlib -o Widgets
dotnet add Widgets package RecursiveAI.OneUI

Both projects target net10.0. In the app’s .csproj, add the settings below inside <Project>. If the install command already added a package reference, keep only one and set its version to match the example:

XML
<PropertyGroup>
  <OneUIMarkupGlob>false</OneUIMarkupGlob>
</PropertyGroup>
<ItemGroup>
  <PackageReference
    Include="RecursiveAI.OneUI"
    Version="1.1.0" />
  <ProjectReference
    Include="Widgets/Widgets.csproj" />
  <Compile Remove="Widgets/**/*.cs" />
  <AdditionalFiles Include="Checkout.html" />
</ItemGroup>

The Compile rule keeps the app from compiling the nested library’s C# files as its own. Turning off the app’s markup glob and listing Checkout.html keeps the library’s template out of the app’s build. If the projects are siblings, you can omit those rules and let OneUI pick up each template automatically.

Checkout.cs in the app provides the partial class the generator pairs with the template:

C#
public partial class Checkout { }

Widgets/PriceBadge.cs declares the library’s required input. OneUI’s generator implements the partial property and builds the component:

C#
using OneUI.Components;
 
namespace Widgets;
 
public partial class PriceBadge
{
    [Param(Required = true)]
    public partial int Dollars { get; set; }
}

Widgets/PriceBadge.html turns the input into text:

HTML
<span>Price: ${Dollars}</span>

Program.cs mounts the checkout in an in-memory document and prints the assembly that supplies the badge:

C#
using OneUI.Components;
using OneUI.Components.Reference;
using Widgets;
 
var dom = new ReferenceDocument();
var ui = new UiContext(dom);
ui.Mount(new Checkout());
Console.WriteLine(dom.ToHtml());
Console.WriteLine($"Badge DLL: {typeof(PriceBadge).Assembly.GetName().Name}.dll");
Text
<span>Price: $25</span>
Badge DLL: Widgets.dll

If you omit Dollars, the app fails to build with OUI0061, even though the parameter was declared in the library, not the app.

Variations

Change the literal in Checkout.html to show a different price. The compiler converts "40" to the integer the parameter expects:

HTML
<PriceBadge Dollars="40" />

The short answer

Reference the project or NuGet package that contains the compiled OneUI component, then use its class name as a tag. The consumer’s markup compiler reads the component’s parameter declarations from the referenced assembly. You don’t need to copy its source or register its tag at runtime.

How do you share UI components between C# projects?

Use a ProjectReference when the app builds alongside the library, as in the example. Use a PackageReference when you consume a released build of that library. The tag is <PriceBadge … /> in either case: it is the class name, not the DLL name or a namespace-qualified C# type. Give exported components a public, non-generic class and a public parameterless constructor. Internal classes only count if they’re visible to the app through InternalsVisibleTo.

OneUI’s generator compiles the library’s template in the library build. If you put a stylesheet next to that template, the generated code carries the scoped CSS, and the document adds it the first time it creates the component. The consuming project only needs its own template and the reference. Keep component source in the library so changes to it benefit each app that builds against the updated library.

Are parameters checked when the component is in a different DLL?

Yes. The compiler checks the names, required parameters, types, and declared template slots from the referenced assembly’s metadata. An omitted Dollars triggers the missing-parameter diagnostic; a literal that can’t be converted to an int triggers OUI0062. Braced expressions are read from the app component’s scope, not the library component’s scope.

If two referenced libraries both export PriceBadge and the app has no component by that name, the compiler reports OUI0064 at the tag. A component declared in the app itself takes precedence over either reference. If you control the libraries and want to use both badges, give the exported classes distinct names; putting them in separate namespaces doesn’t make their short tag names distinct. See the markup reference and diagnostics.

All oneui recipes