Back to Uno

Preventing Window Closing

doc/articles/features/app-close-handler.md

6.6-release-branch-cut1.7 KB
Original Source

Preventing Window Closing

[!TIP] This article covers Uno Platform–specific behavior. For the full API documentation, see AppWindow.Closing Event.

The AppWindow.Closing API lets you respond to or prevent standard app window closing requests—such as clicking the window’s close button or pressing <kbd>Alt</kbd>+<kbd>F4</kbd>. Note that this does not block the user from terminating the app by force (e.g., via Task Manager or kill).

Platform Support

FeatureWindows App SDKAndroidiOSWeb (WASM)Desktop (Windows)Desktop (macOS)Desktop (Linux)
AppWindow.Closing✔️✔️✔️✔️

[!NOTE] On platforms where this feature is not supported, the Closing event will still be raised, but setting args.Cancel = true has no effect.

Usage Example

csharp
MyWindow.AppWindow.Closing += OnAppWindowClosing;

private void OnAppWindowClosing(AppWindow sender, AppWindowClosingEventArgs args)
{
    // Replace with your own logic, such as checking for unsaved changes
    bool cancelClose = ShouldWindowStayOpen();

    // Cancel the close request if needed
    args.Cancel = cancelClose;
}

[!IMPORTANT] The AppWindow.Closing event must be handled synchronously. Asynchronous operations (e.g., showing a ContentDialog) are not allowed and will not delay the closing process.