{"record":{"id":"fb77b3f900352128","repo":"dotnet/maui","slug":"call-forms-init-before-this-fb77b3","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/Windows/PageExtensions.cs","lineNumber":57,"sourceCode":"\t\t\t}\n\n\t\t\tRoot.Content = frameworkElement;\n\t\t}\n\t}\n\n\t[System.Obsolete]\n\tpublic static class PageExtensions\n\t{\n\t\tpublic static FrameworkElement CreateFrameworkElement(this ContentPage contentPage)\n\t\t{\n\t\t\treturn contentPage.ToFrameworkElement();\n\t\t}\n\n\t\tinternal static FrameworkElement ToFrameworkElement(this VisualElement visualElement)\n\t\t{\n\t\t\tif (!Forms.IsInitialized)\n\t\t\t{\n\t\t\t\tthrow new InvalidOperationException(\"call Forms.Init() before this\");\n\t\t\t}\n\n\t\t\tvar root = new Microsoft.UI.Xaml.Window();\n\n\t\t\t// Yes, this looks awkward. But the page needs to be Platformed or several things won't work\n\t\t\tnew WindowsPlatform(root);\n\n\t\t\tvar renderer = visualElement.GetOrCreateRenderer();\n\n\t\t\tif (renderer == null)\n\t\t\t{\n\t\t\t\tthrow new InvalidOperationException($\"Could not find or create a renderer for {visualElement}\");\n\t\t\t}\n\n\t\t\tvar frameworkElement = renderer.ContainerElement;\n\n\t\t\tframeworkElement.Loaded += (sender, args) =>\n\t\t\t{","sourceCodeStart":39,"sourceCodeEnd":75,"githubUrl":"https://github.com/dotnet/maui/blob/f377ff1c5ee04d334d8a925f50c83a6b7afddf03/src/Compatibility/Core/src/Windows/PageExtensions.cs#L39-L75","documentation":"Thrown as InvalidOperationException by PageExtensions.ToFrameworkElement when Forms.IsInitialized is false. The embedding API (CreateFrameworkElement / ToFrameworkElement) needs the full Forms infrastructure — registrar, platform services, renderers — to be bootstrapped via Forms.Init() before any page can be platformed.","triggerScenarios":"Calling contentPage.CreateFrameworkElement() or visualElement.ToFrameworkElement() before Forms.Init() has been called in the application's startup sequence.","commonSituations":"Calling embedding APIs from a non-standard entry point (e.g., a library constructor, a static initializer, or a design-time preview) before the application has run its Forms.Init bootstrap. Also when embedding is used outside a FormsApplicationPage lifecycle.","solutions":["Call Forms.Init() (or Forms.Init(assemblyList)) at application startup before any CreateFrameworkElement call.","If using FormsApplicationPage, ensure its OnStartup/Initialize completes before embedding pages.","Add a guard: check Forms.IsInitialized before calling ToFrameworkElement."],"exampleFix":"// before\nvar fe = contentPage.CreateFrameworkElement(); // throws\n\n// after\nif (!Forms.IsInitialized)\n    Forms.Init();\nvar fe = contentPage.CreateFrameworkElement();","handlingStrategy":"validation","validationCode":"if (!Forms.IsInitialized)\n    Forms.Init();\nvar fe = contentPage.CreateFrameworkElement();","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Call Forms.Init() early in application startup, before any embedding or renderer calls.","Guard embedding code with a Forms.IsInitialized check."],"tags":["wpf","maui-compatibility","forms-init","bootstrap","page-extensions"],"backgroundTag":null,"analyzedSha":"f377ff1c5ee04d334d8a925f50c83a6b7afddf03","analyzedAt":"2026-08-13T14:26:18.069Z","schemaVersion":2},"datasetVersion":"2026-08-13T19:17:28.613Z"}