dotnet add package RecursiveAI.OneUIRequires .NET 10 (net10.0); .NET 9 and earlier aren’t supported.
using OneUI.Components; sealed class Greeting : Component { protected override void Build( Scope scope, int parent, int before) => scope.Insert(parent, scope.Text("Hello"), before); }
The example is the full program below: unzip it and dotnet run.
This component inserts the text Hello without an HTML or CSS file. To try it, use the runner below and mount new Greeting() instead of new Counter(), then print doc.ToHtml() rather than querying or clicking a button.
The full program
The example extends the same pattern to a button that updates its label. Replace the default Program.cs with the runner below. It mounts Counter in an in-memory document, reads the initial label, and clicks the button.
Counter.cs:
using OneUI.Components; sealed class Counter : Component { int _count; const ulong CountBit = 1; // 0 is the state slot index. // CountBit = 1 << 0 is its mask. void Increment() => SetState(ref _count, _count + 1, 0); protected override void Build( Scope scope, int parent, int before) { int button = scope.Element("button"); int label = scope.Text(CountBit, () => $"Count: {_count}"); scope.Listen<MouseEvent>( button, "click", _ => Increment()); scope.Insert(button, label); scope.Insert(parent, button, before); } }
Program.cs:
using OneUI.Components; using OneUI.Components.Reference; var doc = new ReferenceDocument(); var ui = new UiContext(doc); doc.Context = ui; ui.Mount(new Counter()); Console.WriteLine(doc.Text("button")); doc.Click("button"); Console.WriteLine(doc.Text("button"));
dotnet run prints:
Count: 0 Count: 1
If you create a node but don’t insert it, it stays detached and won’t appear in the mounted component.
Variations
For a counter that adds five on each click, replace Increment:
void Increment() => SetState(ref _count, _count + 5, 0);
Related recipes
The short answer
Derive from Component, override Build, and use Scope to create nodes, wire events and insert the nodes into the component tree. The counter uses the same document API as a generated OneUI markup component, but there is no template or partial class. You can mount it in the reference document or use a UI host that applies OneUI change sets.
How do you create UI elements programmatically in C#?
scope.Element("button") gives you the button’s node ID. scope.Text(...) gives you a separate text node. Insert the label into the button, then insert the button into parent using the before argument supplied to Build. Using that insertion point lets the caller decide where the component belongs among its siblings. Creating a node alone doesn’t place it anywhere.
How do you update the UI when C# state changes?
A text node with a binding has a dependency mask. Here, CountBit is 1, so the binding reads state slot 0. The last argument to SetState is the index 0, not the mask 1. When the click changes that slot, the text binding runs again. Updating _count directly wouldn’t notify OneUI, and a binding that listens to the wrong bit won’t update when you expect. Use separate slots for values that can change independently, such as a count and an enabled flag.
If you use markup, the compiler tracks those dependencies for you. When you build the component in C#, keep the state indexes and the matching masks next to the fields they describe. See the programmatic component API for the other Scope methods available in Build.