Call Activate() on a window your app owns. It isn’t an API for focusing an arbitrary window belonging to another process. It asks the desktop to bring it forward and send keyboard input to it.
dotnet add package RecursiveAI.OnePlatformRequires .NET 10 (net10.0); .NET 9 and earlier aren’t supported.
using OnePlatform; using Kind = OnePlatform.PlatformEventKind; using var app = PlatformApp.Create(); var target = app.CreateWindow(new() { Title = "Document" }); var control = app.CreateWindow(new() { Title = "Click to focus Document" }); while (true) { app.PumpEvents(100); while (app.TryGetEvent(out var e)) { if (e.Kind is Kind.WindowCloseRequested or Kind.QuitRequested) return; if (e.WindowId == control.Id && e.Kind == Kind.PointerButtonDown && e.Button == MouseButton.Left) { target.Activate(); Console.WriteLine( "Activation requested."); } if (e.WindowId == target.Id && e.Kind == Kind.WindowFocusChanged && e.Focused) Console.WriteLine( "Document has focus."); } }
The desktop can refuse an activation request. An unsupported window or backend can throw ActivationUnavailableException; on Wayland, for example, the compositor may lack xdg_activation_v1.
Paste the code above into Program.cs of a new dotnet new console project and dotnet run, or unzip the example and dotnet run.
Click inside Click to focus Document. Click either window’s close button (×) to quit. On Linux (X11), the example printed these lines when the click brought Document forward:
Activation requested. Document has focus.
Variations
When handling a launcher action rather than a click, pass along the Wayland token supplied for that launch. Here target is the same existing window from the example:
var token = Environment.GetEnvironmentVariable( "XDG_ACTIVATION_TOKEN"); target.Activate(token);
Related recipes
The short answer
Call target.Activate() on the UI thread, where target is an open OnePlatform window you already created. The call asks the desktop to raise the window, restore it if minimized, and give it keyboard focus. Run it from a user action such as a click.
How do you bring a C# window to the front without keeping it on top?
Activation is a one-time request. After it succeeds, the user can switch to another window as usual. SetKeepAbove(true) changes stacking policy instead: it keeps an ordinary window above others when the backend supports that option. That’s not needed to return to a document or open a window the user selected from a list. Keep a reference to the existing window and request activation rather than creating a second copy.
A return from Activate doesn’t mean the target has the keyboard. If you need to know when it’s ready for input, watch for WindowFocusChanged with Focused set and the target’s WindowId, as the example does. The example logs focus changes caused by the desktop as well as changes from the click; you may see a focus line during startup.
Why doesn’t a request to bring the window forward always work?
The window system decides whether the request is backed by user input. On X11, OnePlatform sends the window manager the time of the latest key or button press. On Wayland it can use a launch token, or ask for one tied to recent input. Even with support for activation, a Wayland compositor can decline the request. It may show a taskbar hint instead. Windows also restricts which processes can claim the foreground.
If a running app gets a launch request from a second instance, OnePlatform’s SingleInstance can forward a launcher token. The running instance can pass ActivationRequest.ActivationToken to Activate instead of relying on an earlier click. The desktop window docs describe what each backend accepts.