{"record":{"id":"080173d37a7bae93","repo":"dotnet/maui","slug":"call-forms-init-before-this-080173","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/MacOS/Extensions/PageExtensions.cs","lineNumber":11,"sourceCode":"using System;\nusing AppKit;\n\nnamespace Microsoft.Maui.Controls.Compatibility.Platform.MacOS\n{\n\tpublic static class PageExtensions\n\t{\n\t\tpublic static NSViewController CreateViewController(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\t\t\treturn result.ViewController;\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/MacOS/Extensions/PageExtensions.cs#L1-L28","documentation":"PageExtensions.CreateViewController requires the Forms platform layer to be initialized (Forms.IsInitialized) before it can build a view controller, because it needs registered renderers and the platform backend set up. Calling it before Forms.Init() means there is no platform context to create the controller from, so it throws InvalidOperationException. Forms.Init() is normally called in the AppDelegate/Program startup.","triggerScenarios":"Invoking somePage.CreateViewController() (a macOS extension method) before Forms.Init() has run. Typically in a unit test, a non-standard host, or when CreateViewController is called from a static initializer / early startup path.","commonSituations":"Calling CreateViewController from a context that runs before the app delegate's FinishedLaunching/DidFinishLaunching calls Forms.Init(). Reusing the extension in a test harness that never calls Forms.Init(). Upgrading where the Init call was moved or removed.","solutions":["Call Forms.Init() in your NSApplicationDelegate before any CreateViewController call: Forms.Init(); var vc = page.CreateViewController();","If in a test, call Forms.Init() (or the test host's equivalent) in the setup phase first.","Reorder startup so Forms.Init() precedes the first CreateViewController."],"exampleFix":"// before\nvar vc = myPage.CreateViewController(); // throws: not initialized\n\n// after\nForms.Init();\nvar vc = myPage.CreateViewController();","handlingStrategy":"validation","validationCode":"if (!Forms.IsInitialized)\n    Forms.Init();\nvar vc = page.CreateViewController();","typeGuard":"static bool FormsReady() => Forms.IsInitialized;","tryCatchPattern":null,"preventionTips":["Call Forms.Init() exactly once at app startup.","In tests, initialize Forms in the setup fixture.","Do not call CreateViewController from static initializers."],"tags":["macos","initialization","forms","lifecycle","maui","xamarin"],"backgroundTag":null,"analyzedSha":"f377ff1c5ee04d334d8a925f50c83a6b7afddf03","analyzedAt":"2026-08-13T14:26:18.069Z","schemaVersion":2},"datasetVersion":"2026-08-13T19:17:28.613Z"}