Recursive AI

Apply C# UI changes with a custom render sink

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.

OneUI sends UI edits as an ordered change set to a sink you supply, which applies them to a native tree or browser DOM, or turns them into text.

Terminal
dotnet add package RecursiveAI.OneUI

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

This minimal ConsoleSink.cs uses ReferenceDocument, OneUI’s built-in in-memory tree, to apply each change set and render the tree as HTML.

C#
using OneUI.Components;
using OneUI.Components.Reference;
 
sealed class ConsoleSink : IChangeSink
{
    readonly ReferenceDocument doc = new();
 
    public void Apply(ChangeSet batch)
    {
        doc.Apply(batch);
        System.Console.WriteLine(doc.ToHtml());
    }
}

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

The example prints Waiting | 0 items, then Ready | 2 items, once per commit.

The full program

To build by hand, run dotnet new console with the .NET 10 SDK, then run the install command above. Replace the starter Program.cs with the complete listing below, which includes its own ConsoleSink class. If you saved the minimal ConsoleSink.cs above, remove it to avoid defining the class twice. No component template is needed.

The program passes the sink to UiContext, creates two text nodes in one batch, and updates both in the next. The sink logs the batch number and node IDs before printing the view.

C#
using OneUI.Components;
using OneUI.Components.Reference;
 
var ui = new UiContext(new ConsoleSink());
int left = 0, right = 0;
ui.Batch(() =>
{
    left = ui.CreateText("Waiting");
    right = ui.CreateText(" | 0 items");
    ui.Insert(ChangeSetProtocol.RootNode, left);
    ui.Insert(ChangeSetProtocol.RootNode, right);
});
ui.Batch(() =>
{
    ui.SetText(left, "Ready");
    ui.SetText(right, " | 2 items");
});
 
sealed class ConsoleSink : IChangeSink
{
    private readonly ReferenceDocument doc = new();
 
    public void Apply(ChangeSet batch)
    {
        doc.Apply(batch);
        Console.WriteLine($"Commit {batch.Sequence} ({batch.Changes.Count} changes)");
        foreach (var change in batch.Changes)
        {
            string target = change.Kind == ChangeKind.Insert
                ? $"node {change.Other} into {change.Node}"
                : $"node {change.Node}";
            Console.WriteLine($"  {change.Kind}: {target}");
        }
        Console.WriteLine($"  View: {doc.ToHtml()}");
    }
}
Text
Commit 1 (4 changes)
  CreateText: node 2
  CreateText: node 3
  Insert: node 2 into 1
  Insert: node 3 into 1
  View: Waiting | 0 items
Commit 2 (2 changes)
  SetText: node 2
  SetText: node 3
  View: Ready | 2 items

A change that refers to an unknown node can fail with ChangeSetException; OneUI doesn’t retry the commit.

Variations

In the second batch, update the right-hand node first. The SetText operations arrive in the new order, but the view still reads Ready | 2 items because its node order hasn’t changed:

C#
ui.Batch(() =>
{
    ui.SetText(right, " | 2 items");
    ui.SetText(left, "Ready");
});

The short answer

Implement IChangeSink.Apply(ChangeSet) and pass the sink to new UiContext(sink). Apply the mutations in the order they appear, then publish the result once for the complete batch. If you need an HTML snapshot rather than a new tree implementation, let ReferenceDocument handle the mutations and add your own output after it applies them.

How do you batch UI updates in a custom renderer?

Use UiContext.Batch to group related updates. The outermost batch recomputes dirty bindings and sends one ordered set to the sink. In the example, “Ready” and “2 items” arrive together in commit 2. The sink doesn’t publish a view with the new status and the old count. A component event sent through UiContext.Dispatch also runs as a batch.

How do you preserve node identity when a UI updates?

Key the backend’s nodes by the IDs in the change set rather than by their position or text. The sink owns root ID 1. Other IDs start at 2 and aren’t reused within a context. CreateText gives a node its ID before insertion. In an Insert, Node identifies the parent and Other identifies the child. SetText uses the existing text node’s ID; it doesn’t create or move the node. That’s why both commits can refer to nodes 2 and 3.

The same rule matters when a renderer reorders items: Move keeps the existing node and its listeners, while Remove destroys a subtree and its listeners. A renderer that builds its own tree also has to handle attributes, styles, namespaces, focus, and scrolling. The change-set protocol specifies which fields to read for each operation.

Writing your own tree

These protocol details apply only if your sink applies changes itself instead of delegating to ReferenceDocument.

ChangeSet.Sequence starts at one and increases per context, so a consumer can detect a missed commit. ChangeSet.Version is separate: it says which protocol the set uses. In OneUI 1.1.0 the protocol is version 3, which adds ScrollIntoView, ScrollTo, and ScrollBy. A sink that implements version 3 itself must accept versions 1 and 2 and refuse versions above 3. Apply each scroll in order, using the layout as it stands after the preceding changes, so it can target a node inserted earlier in the same batch. If your backend reports events, deliver any events raised during application after the batch, not from inside Apply.

All oneui recipes