{"record":{"id":"3de26d630bfed44c","repo":"dotnet/maui","slug":"call-forms-init-before-this-3de26d","errorCode":null,"errorMessage":"call Forms.Init() before this","messagePattern":"call Forms\\.Init\\(\\) before this","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"src/Compatibility/Core/src/GTK/Extensions/PageExtensions.cs","lineNumber":10,"sourceCode":"using System;\n\nnamespace Microsoft.Maui.Controls.Compatibility.Platform.GTK.Extensions\n{\n\tpublic static class PageExtensions\n\t{\n\t\tpublic static GtkFormsContainer CreateContainer(this Page view)\n\t\t{\n\t\t\tif (!Forms.IsInitialized)\n\t\t\t\tthrow new InvalidOperationException(\"call Forms.Init() before this\");\n\n\t\t\tif (!(view.RealParent is Application))\n\t\t\t{\n\t\t\t\tApplication app = new DefaultApplication();\n\t\t\t\tapp.MainPage = view;\n\t\t\t}\n\n\t\t\tvar result = new Platform();\n\t\t\tresult.SetPage(view);\n\n\t\t\treturn result.PlatformRenderer;\n\t\t}\n\n\t\tclass DefaultApplication : Application\n\t\t{\n\t\t}\n\t}\n}","sourceCodeStart":1,"sourceCodeEnd":28,"githubUrl":"https://github.com/dotnet/maui/blob/f377ff1c5ee04d334d8a925f50c83a6b7afddf03/src/Compatibility/Core/src/GTK/Extensions/PageExtensions.cs#L1-L28","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","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."],"exampleFix":"// before\nvar window = myPage.CreateContainer(); // throws — Forms not initialized\n// after\nGtk.Application.Init();\nForms.Init(); // GTK init\nvar window = myPage.CreateContainer();","handlingStrategy":"validation","validationCode":"static GtkFormsContainer SafeCreateContainer(Page p)\n{\n    if (!Forms.IsInitialized)\n        throw new InvalidOperationException(\"Call Forms.Init() before CreateContainer.\");\n    return p.CreateContainer();\n}","typeGuard":"static bool FormsReady => Forms.IsInitialized;","tryCatchPattern":null,"preventionTips":["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."],"tags":["gtk","startup","init-order","forms","maui-compat"],"backgroundTag":null,"analyzedSha":"f377ff1c5ee04d334d8a925f50c83a6b7afddf03","analyzedAt":"2026-08-13T14:26:18.069Z","schemaVersion":2},"datasetVersion":"2026-08-13T19:17:28.613Z"}