{"record":{"id":"572b513668db9d13","repo":"dotnet/maui","slug":"setting-a-parent-on-application-is-invalid","errorCode":null,"errorMessage":"Setting a Parent on Application is invalid.","messagePattern":"Setting a Parent on Application is invalid\\.","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"src/Controls/src/Core/Application/Application.cs","lineNumber":352,"sourceCode":"\t\t}\n\n\t\tpublic event EventHandler<Page>? PageAppearing;\n\n\t\tpublic event EventHandler<Page>? PageDisappearing;\n\n\t\t/// <inheritdoc/>\n\t\tpublic IPlatformElementConfiguration<T, Application> On<T>() where T : IConfigPlatform\n\t\t{\n\t\t\treturn _platformConfigurationRegistry.Value.On<T>();\n\t\t}\n\n\t\tprotected virtual void OnAppLinkRequestReceived(Uri uri)\n\t\t{\n\t\t}\n\n\t\tprotected override void OnParentSet()\n\t\t{\n\t\t\tthrow new InvalidOperationException(\"Setting a Parent on Application is invalid.\");\n\t\t}\n\n\t\tprotected virtual void OnResume()\n\t\t{\n\t\t}\n\n\t\tprotected virtual void OnSleep()\n\t\t{\n\t\t}\n\n\t\tprotected virtual void OnStart()\n\t\t{\n\t\t}\n\n\t\tinternal static void ClearCurrent() => Current = null;\n\n\t\tinternal static bool IsApplicationOrNull(object? element) =>\n\t\t\telement == null || element is IApplication;","sourceCodeStart":334,"sourceCodeEnd":370,"githubUrl":"https://github.com/dotnet/maui/blob/f377ff1c5ee04d334d8a925f50c83a6b7afddf03/src/Controls/src/Core/Application/Application.cs#L334-L370","documentation":"Thrown by Application.OnParentSet, which is overridden to unconditionally throw because Application is the root of the visual/logical tree and is not allowed to have a parent. Any attempt to assign a parent to an Application instance is rejected at the point the Parent property would commit the change.","triggerScenarios":"Code sets application.Parent = something (or adds the Application to a parent element) so that the Element Parent setter eventually calls OnParentSet, which throws InvalidOperationException.","commonSituations":"Manually wiring Application into a custom host; misusing Element helpers (e.g. AddLogicalChild / parenting helpers) on the Application; porting code that parented a page-like root onto the new Application model.","solutions":["Do not set Parent on Application; restructure so the Application stays the root and children are added via Windows.","If you need a logical container, put it on a Window/Page rather than on Application.","Audit any reflection or generic Element helpers that call set_Parent to ensure they skip Application instances."],"exampleFix":"// before\napp.Parent = hostElement; // throws\n// after\n// Application is the root; do not assign a parent.\n// Move hostElement under a Window/Page instead.","handlingStrategy":"validation","validationCode":"// Never parent an Application; if a helper sets Parent generically, skip Application instances.\nstatic void SafeSetParent(Element child, Element parent)\n{\n    if (child is Application) throw new InvalidOperationException(\"Application cannot have a parent.\");\n    child.Parent = parent;\n}","typeGuard":"static bool CanHaveParent(Element e) => e is not Application;","tryCatchPattern":null,"preventionTips":["Treat Application as the immutable root of your app; never assign its Parent.","Audit generic Element helpers/reflection that set Parent to exclude Application.","Put logical containers on Window/Page, not on Application."],"tags":["application","parenting","invalid-operation","logical-tree"],"backgroundTag":null,"analyzedSha":"f377ff1c5ee04d334d8a925f50c83a6b7afddf03","analyzedAt":"2026-08-13T14:26:18.069Z","schemaVersion":2},"datasetVersion":"2026-08-13T19:17:28.613Z"}