Recursive AI

Bind an on/off switch in a C# desktop UI

OneApp is a free, open-source framework for apps in .NET whose UI is HTML and CSS, drawn natively with no web view on Windows, macOS, Linux, iOS and Android, and run in the browser, from Recursive AI. MIT or Apache-2.0, no native DLLs to ship.

This example opens a native window. For phone and tablet builds, see OneApp’s mobile setup.

OneApp opens the window; OneApp.Kit supplies Switch; OneUI compiles your markup.

Terminal
dotnet new console --framework net10.0
dotnet add package RecursiveAI.OneApp.Kit
dotnet add package RecursiveAI.OneApp
dotnet add package RecursiveAI.OneUI

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

HTML
<Label For="sounds">Sound effects</Label>
<Switch Id="sounds" Checked="{SoundsOn}"
        OnCheckedChange="{SetSounds}" />

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

Put this markup in SettingsPage.html. SettingsPage.cs defines both names: SoundsOn holds the on/off value and SetSounds updates it. The window starts at Off; flipping the switch updates both the switch and the status below it.

The full program

The markup compiler generates the other half of SettingsPage, including the implementation of its [State] property.

XML
<ItemGroup>
  <PackageReference
    Include="RecursiveAI.OneApp.Kit"
    Version="1.1.1" />
  <PackageReference
    Include="RecursiveAI.OneApp"
    Version="1.1.1" />
  <PackageReference
    Include="RecursiveAI.OneUI"
    Version="1.1.0" />
</ItemGroup>

SettingsPage.html:

HTML
<Theme>
  <main style="padding: 24px">
    <div style="display: flex; gap: 8px;
                align-items: center">
      <Label For="sounds">Sound effects</Label>
      <Switch Id="sounds" Checked="{SoundsOn}"
              OnCheckedChange="{SetSounds}" />
    </div>
    <p id="status">
      {SoundsOn ? "On" : "Off"}
    </p>
  </main>
</Theme>

SettingsPage.cs:

C#
using OneUI.Components;
 
public partial class SettingsPage
{
    [State]
    private partial bool SoundsOn { get; set; }
 
    private void SetSounds(bool value)
    {
        SoundsOn = value;
        Console.WriteLine(
            SoundsOn ? "On" : "Off");
    }
}

Program.cs opens the window with the SettingsPage component defined above:

C#
using OneApp;
 
var options = new AppOptions(new SettingsPage())
{
    Title = "Settings",
    Width = 340,
    Height = 190,
};
using var host = AppHost.Create(options);
host.Run();

Click the switch, then press Space while it has focus. This prints the following (checked on Linux (X11)):

Text
On
Off

The Settings window with the Sound effects switch and the status On

If the switch snaps back or seems stuck, check that the callback assigns its argument to the same [State] property you pass to Checked.

Variations

Use the small switch in a compact row. Keep SoundsOn and SetSounds from SettingsPage.cs:

HTML
<Switch Id="sounds" Checked="{SoundsOn}"
        OnCheckedChange="{SetSounds}"
        Size="{OneApp.Kit.ControlSize.Sm}" />

To start on, add this method to SettingsPage.cs, which defines SoundsOn:

C#
protected override void OnMounted()
    => SoundsOn = true;

The short answer

Pass a C# bool to Switch.Checked and a callback to OnCheckedChange. When the user flips the switch, accept the requested value by assigning it to your [State] property. The property is the source of truth for the switch and for anything else that depends on the setting. The sample keeps it in memory; it doesn’t play sound or save a preference.

How do I make a button work as an on/off switch?

Use Switch instead of a text field or a button whose caption changes from “Enable” to “Disable.” OneApp Kit renders a button with role="switch" and reports the current value through aria-checked. Match Label.For to Switch.Id to give it a visible name. If there’s no visible label, set AriaLabel. Keep the name the same in both states: the WAI-ARIA switch pattern separates what the switch controls from whether it’s on.

With OnCheckedChange set, the switch asks the parent for the opposite value; it doesn’t commit the change itself. If the parent leaves Checked alone, the switch keeps showing that value. Without the callback the control toggles its own state, which is fine for a standalone switch but doesn’t update a setting that the rest of your app reads. Set Disabled when the setting isn’t available; a disabled switch doesn’t call the handler.

When should I use a toggle switch instead of a checkbox?

Use a switch when a yes/no preference changes immediately, such as showing line numbers in an editor. If a user is filling out a form and still has to click Apply, a checkbox can make it clearer that the selection is pending. See the toggle switch design guidelines. If the switch is also meant to survive a restart, load the saved boolean into the page’s state and save accepted changes from the handler. Merely binding Checked doesn’t persist anything.

All oneapp recipes