dotnet/maui · error · InvalidOperationException
call Forms.Init() before this
Error message
call Forms.Init() before this
What it means
PageExtensions.CreateContainer (GTK) builds the hosting container for a Page. It requires the Forms compatibility runtime to be initialized (Forms.IsInitialized) and throws InvalidOperationException('call Forms.Init() before this') otherwise, because the platform/handlers it constructs depend on initialized Forms state.
Source
Thrown at src/Compatibility/Core/src/GTK/Extensions/PageExtensions.cs:10
using System;
namespace Microsoft.Maui.Controls.Compatibility.Platform.GTK.Extensions
{
public static class PageExtensions
{
public static GtkFormsContainer CreateContainer(this Page view)
{
if (!Forms.IsInitialized)
throw new InvalidOperationException("call Forms.Init() before this");
if (!(view.RealParent is Application))
{
Application app = new DefaultApplication();
app.MainPage = view;
}
var result = new Platform();
result.SetPage(view);
return result.PlatformRenderer;
}
class DefaultApplication : Application
{
}
}
}View on GitHub (pinned to f377ff1c5e)
Solutions
- Call Forms.Init() (GTK overload) early in the GTK app entry, before creating/showing any Page or container.
- Ensure the Init call precedes any CreateContainer/Platform.SetPage usage.
- If embedding, init Forms at application startup rather than on first use.
Example fix
// before var window = myPage.CreateContainer(); // throws — Forms not initialized // after Gtk.Application.Init(); Forms.Init(); // GTK init var window = myPage.CreateContainer();
Defensive patterns
Strategy: validation
Validate before calling
static GtkFormsContainer SafeCreateContainer(Page p)
{
if (!Forms.IsInitialized)
throw new InvalidOperationException("Call Forms.Init() before CreateContainer.");
return p.CreateContainer();
} Type guard
static bool FormsReady => Forms.IsInitialized;
Prevention
- Call Forms.Init() at GTK app startup, before any Page/Platform usage.
- Add a startup assertion that Forms.IsInitialized is true.
- Keep init ordering: Gtk.Application.Init() -> Forms.Init() -> create pages.
When it happens
Trigger: Calling page.CreateContainer() (or the GTK hosting entry that uses it) before Forms.Init(...) has run. Typical of an app whose Main/entry method tries to show a Page before bootstrapping Forms.
Common situations: GTK app entry point invoking CreateContainer on a Page before calling Forms.Init(); reordered startup code; a test or design-time host that bypasses Forms.Init; migration from a template that inits Forms lazily.
Related errors
- call GtkThemes.Init() before this
- Unable to find Application Services
- Unable to find Application Services
- MinimumDate must be lower than MaximumDate
- MaximumDate must be greater than MinimumDate
AI-assisted analysis of dotnet/maui@f377ff1c5e (2026-08-13).
Data as JSON: /api/errors/3de26d630bfed44c.
Report an issue: GitHub.