{"record":{"id":"6fd34b44873fcd8d","repo":"dotnet/maui","slug":"call-forms-init-before-this-6fd34b","errorCode":null,"errorMessage":"call Forms.Init() before this","messagePattern":"call Forms\\.Init\\(\\) before this","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"critical","filePath":"src/Compatibility/Core/src/iOS/PageExtensions.cs","lineNumber":14,"sourceCode":"using System;\nusing System.Linq;\nusing ObjCRuntime;\nusing UIKit;\n\nnamespace Microsoft.Maui.Controls.Compatibility\n{\n\t[System.Obsolete]\n\tpublic static class PageExtensions\n\t{\n\t\tpublic static UIViewController CreateViewController(this Page page)\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 (!(page.RealParent is Application))\n\t\t\t{\n\t\t\t\tApplication app = new EmbeddedApplication();\n\t\t\t\tapp.MainPage = page;\n\t\t\t}\n\n\t\t\tvar result = new Platform.iOS.Platform();\n\t\t\tresult.SetPage(page);\n\t\t\treturn result.ViewController;\n\t\t}\n\n\t\tsealed internal class EmbeddedApplication : Application, IDisposable\n\t\t{\n\t\t\tpublic void Dispose()\n\t\t\t{\n\t\t\t\tCleanUp();\n\t\t\t}","sourceCodeStart":1,"sourceCodeEnd":32,"githubUrl":"https://github.com/dotnet/maui/blob/f377ff1c5ee04d334d8a925f50c83a6b7afddf03/src/Compatibility/Core/src/iOS/PageExtensions.cs#L1-L32","documentation":"PageExtensions.CreateViewController throws InvalidOperationException(\"call Forms.Init() before this\") when Forms.IsInitialized is false. Embedding a Page into a native UIViewController requires the Forms platform (renderers, registrar) to be initialized first via Forms.Init().","triggerScenarios":"Calling PageExtensions.CreateViewController() before Forms.Init() in the AppDelegate or before any Forms startup; using PageExtensions in a non-Forms hosted context.","commonSituations":"Integrating a single Forms Page into a native iOS app but forgetting Forms.Init() in FinishedLaunching; calling CreateViewController from a different startup path that runs before initialization; library code that assumes Forms is up.","solutions":["Call Forms.Init() in FinishedLaunching before any CreateViewController usage.","Guard with Forms.IsInitialized check and initialize on demand if needed.","Ensure the correct platform-specific Forms.Init overload (with the renderer assemblies) is invoked."],"exampleFix":"// before\nvar vc = page.CreateViewController();\n// after\nif (!Forms.IsInitialized)\n    Forms.Init();\nvar vc = page.CreateViewController();","handlingStrategy":"validation","validationCode":"if (!Forms.IsInitialized)\n    Forms.Init();\nvar vc = page.CreateViewController();","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Call Forms.Init() in FinishedLaunching before embedding pages.","Guard with Forms.IsInitialized for late-init scenarios.","Use the correct Forms.Init overload with renderer assemblies."],"tags":["maui","xamarin-forms","ios","initialization","startup","invalidoperation"],"backgroundTag":null,"analyzedSha":"f377ff1c5ee04d334d8a925f50c83a6b7afddf03","analyzedAt":"2026-08-13T14:26:18.069Z","schemaVersion":2},"datasetVersion":"2026-08-13T19:17:28.613Z"}