{"record":{"id":"de59383dc2873a10","repo":"dotnet/maui","slug":"call-forms-init-before-this-de5938","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/WPF/Extensions/PageExtensions.cs","lineNumber":11,"sourceCode":"using System;\nusing System.Windows;\n\nnamespace Microsoft.Maui.Controls.Compatibility.Platform.WPF.Extensions\n{\n\tpublic static class PageExtensions\n\t{\n\t\tpublic static FrameworkElement ToFrameworkElement(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\t{\n\t\t\t\t\tMainPage = view\n\t\t\t\t};\n\n\t\t\t\tvar formsApplicationPage = new FormsApplicationPage();\n\t\t\t\tformsApplicationPage.LoadApplication(app);\n\t\t\t\tvar platform = new Platform(formsApplicationPage);\n\t\t\t\tplatform.SetPage(view);\n\t\t\t}\n\n\t\t\tIVisualElementRenderer renderer = Platform.GetOrCreateRenderer(view);\n\n\t\t\tif (renderer == null)\n\t\t\t{","sourceCodeStart":1,"sourceCodeEnd":29,"githubUrl":"https://github.com/dotnet/maui/blob/f377ff1c5ee04d334d8a925f50c83a6b7afddf03/src/Compatibility/Core/src/WPF/Extensions/PageExtensions.cs#L1-L29","documentation":"PageExtensions.ToFrameworkElement throws InvalidOperationException(\"Call Forms.Init() before this\") if Forms.IsInitialized is false. ToFrameworkElement builds the WPF platform backend (Platform, FormsApplicationPage) on demand, which requires the Forms application to have been initialised first via Forms.Init().","triggerScenarios":"Calling ToFrameworkElement on a Page before invoking Forms.Init() in the WPF host's startup; calling it from a designer, unit test, or static constructor that runs before the host's Main initialises Forms.","commonSituations":"WPF host that wires Pages before Forms.Init(); designer-time instantiation; reordered startup where the first ToFrameworkElement runs ahead of the Init call; multiple entry points (CLI/designer) that skip Init.","solutions":["Call Forms.Init() in the WPF application's OnStartup / Main before any ToFrameworkElement call.","For unit tests or designers, call Forms.Init() in a setup method or guard the call with Forms.IsInitialized.","Centralise Forms initialisation in one place and ensure it runs on the UI thread before Page use.","If embedding in an existing app, hook Application.Startup rather than a Page constructor."],"exampleFix":"// before\nprotected override void OnStartup(StartupEventArgs e)\n{\n    var fe = page.ToFrameworkElement();\n    ...\n}\n\n// after\nprotected override void OnStartup(StartupEventArgs e)\n{\n    Forms.Init();\n    var fe = page.ToFrameworkElement();\n    ...\n}","handlingStrategy":"validation","validationCode":"if (!Forms.IsInitialized) Forms.Init();\nreturn page.ToFrameworkElement();","typeGuard":"static bool FormsReady => Forms.IsInitialized;","tryCatchPattern":"try { return page.ToFrameworkElement(); }\ncatch (InvalidOperationException ex) when (ex.Message.Contains(\"Forms.Init\"))\n{ Forms.Init(); return page.ToFrameworkElement(); }","preventionTips":["Centralise Forms.Init() in OnStartup before any page use.","Guard designer/test entry points with Forms.IsInitialized.","Add a startup-order unit test for the WPF host."],"tags":["wpf","initialization","startup-order","page-extensions"],"backgroundTag":null,"analyzedSha":"f377ff1c5ee04d334d8a925f50c83a6b7afddf03","analyzedAt":"2026-08-13T14:26:18.069Z","schemaVersion":2},"datasetVersion":"2026-08-13T19:17:28.613Z"}