unoplatform/uno · critical · InvalidOperationException
The Window should be created later in the application lifecy
Error message
The Window should be created later in the application lifecycle.
What it means
Thrown in OnWindowCreate (under HAS_UNO && !HAS_UNO_WINUI) when the app's static Current is null at the moment a Window is created. Uno expects the application singleton to be initialized before any Window is constructed; a Window built too early in the lifecycle violates that ordering.
Source
Thrown at src/SamplesApp/SamplesApp.Shared/App.xaml.cs:361
private void ActivateMainWindow()
{
#if DEBUG && (__SKIA__ || __WASM__)
_mainWindow!.EnableHotReload();
#endif
// await Task.Delay(15000); // Artificial delay to simulate asynchronous activation
_mainWindow!.Activate();
_wasActivated = true;
MainWindowActivated?.Invoke(this, EventArgs.Empty);
}
public event EventHandler? MainWindowActivated;
#if HAS_UNO && !HAS_UNO_WINUI
protected override void OnWindowCreated(global::Microsoft.UI.Xaml.WindowCreatedEventArgs args)
{
if (Current is null)
{
throw new InvalidOperationException("The Window should be created later in the application lifecycle.");
}
}
#endif
private void InitializeFrame(string? arguments = null)
{
if (_mainWindow is null)
{
throw new InvalidOperationException("Main window must be initialized before Frame");
}
var rootFrame = _mainWindow.Content as Frame;
// Do not repeat app initialization when the Window already has content,
// just ensure that the window is active
if (rootFrame is null)
{
// Create a Frame to act as the navigation context and navigate to the first pageView on GitHub (pinned to 0418340488)
Solutions
- Defer all Window construction until after the Application is initialized (OnLaunched or later).
- Ensure Current is set (the framework sets it during Application startup) before any new Window().
- If you have a custom entry point, call the standard Uno host build that sets Current first.
- Move window creation out of static constructors and field initializers.
Example fix
// before - window built in a static/field initializer
public static Window Splash = new Window();
// after - build it in OnLaunched
protected override void OnLaunched(LaunchActivatedEventArgs e)
{
var splash = new Window();
splash.Activate();
} Defensive patterns
Strategy: validation
Validate before calling
if (Application.Current is null) throw new InvalidOperationException("Initialize the Application before creating any Window."); Prevention
- Never construct a Window in static/field initializers.
- Create windows in OnLaunched or later, after Current is set.
- Use the standard Uno host build to guarantee initialization order.
When it happens
Trigger: Constructing a Microsoft.UI.Xaml.Window before the Application instance is fully initialized — e.g. creating a window in a static constructor, before OnLaunched, or during host build before Current is assigned.
Common situations: App startup reordering; creating a splash or secondary window during service registration; custom main() that instantiates Window before Application.Run; refactoring that moved window creation earlier.
Related errors
- Main window must be initialized before Frame
- Failed to load Page {e.SourcePageType}: {e.Exception}
- Failed to load {e.SourcePageType.FullName}: {e.Exception}
- Failed to load {e.SourcePageType.FullName}: {e.Exception}
- serviceProvider does not implement IXamlNameResolver
AI-assisted analysis of unoplatform/uno@0418340488 (2026-08-13).
Data as JSON: /api/errors/6878f12d440aa77e.
Report an issue: GitHub.