Recursive AI

Create a native desktop window in C#

OnePlatform is a free, open-source library for windows, input, clipboard, displays and gamepads in .NET from Recursive AI. MIT or Apache-2.0, no native DLLs to ship.

Open an OS-native window from a plain console project on Windows, macOS, or Linux. You don’t need a WinForms or WPF reference or a UseWindowsForms setting.

Terminal
dotnet add package RecursiveAI.OnePlatform

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

C#
using OnePlatform;
 
using var app = PlatformApp.Create();
var window = app.CreateWindow(new() { Title = "Hello" });
Console.WriteLine($"Opened {window.Id} on {app.Backend}");
 
while (true)
{
    app.PumpEvents(100);
    while (app.TryGetEvent(out var evt))
        if (evt.Kind is PlatformEventKind.WindowCloseRequested
            or PlatformEventKind.QuitRequested)
        {
            Console.WriteLine("Closing");
            return;
        }
}

PumpEvents(100) reads native events and waits up to 100 milliseconds when there’s nothing to read. TryGetEvent then drains the queue. return leaves the top-level program, and using disposes the app, closing its windows and disconnecting from the window system. If Linux has no reachable display, automatic backend selection can fall back to headless and no native window appears; the printed backend tells you which one opened.

Paste the code above into Program.cs of a new dotnet new console project and dotnet run, or unzip the example and dotnet run.

A blank window titled Hello opens. Close it to exit. The Linux X11 run printed:

Text
Opened 1 on X11
Closing

Variations

Set the title and initial client size by replacing the CreateWindow call:

C#
var window = app.CreateWindow(new()
{
    Title = "Preview", Width = 640, Height = 360
});

The short answer

To create a native desktop window in C#, add OnePlatform to a plain .NET 10 console project. PlatformApp.Create() selects the backend; CreateWindow() opens the window. Keep calling PumpEvents and draining events until you get WindowCloseRequested or QuitRequested. Dispose the app to release the native window. You don’t need a Windows-specific project file or a UI designer.

How do you create a desktop window from a C# console app?

The console project supplies the entry point; OnePlatform creates a separate native window. The console is where the example writes its status messages, not where it draws the window. Keep the window calls and event loop on the application thread. In this example, the top-level code stays on that thread because it never awaits or hands the loop to a worker.

OnePlatform gives you a window and its events, not controls. To put text and controls in a window, follow OneApp’s getting started guide. For your own drawing, window.NativeHandle gives a renderer the native window or view, while window.Width and window.Height report the client area in physical pixels. See the desktop window options for dialogs, overlays, and popups.

Does the same C# window code run on Windows, macOS, and Linux?

The desktop backends call Win32, Cocoa, Wayland, or X11. The same window creation and event loop can use those backends without separate source files, but window-manager capabilities differ. For instance, the desktop window docs explain why a Wayland application can’t set an ordinary window’s absolute position. If placement matters to your application, check what the backend can do rather than assuming it can move the window to the same desktop coordinates on every OS.

The close request in the example is an event, not an instruction that your handler must obey immediately. If you need to ask about unsaved changes, handle the request before leaving the loop. In a multi-window application, inspect the event’s WindowId so closing a secondary window doesn’t shut down the entire process.

All oneplatform recipes