{"record":{"id":"173208fe30f91058","repo":"PrismLibrary/Prism","slug":"resources-layoutviewhaschildrenexception","errorCode":null,"errorMessage":"Resources.LayoutViewHasChildrenException","messagePattern":"Resources\\.LayoutViewHasChildrenException","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"src/Maui/Prism.Maui/Navigation/Regions/Adapters/LayoutRegionAdapter.cs","lineNumber":37,"sourceCode":"\n    /// <summary>\n    /// Adapts a <see cref=\"Layout\"/> to an <see cref=\"IRegion\"/>.\n    /// </summary>\n    /// <param name=\"region\">The new region being used.</param>\n    /// <param name=\"regionTarget\">The object to adapt.</param>\n    protected override void Adapt(IRegion region, Layout regionTarget)\n    {\n        if (region == null)\n            throw new ArgumentNullException(nameof(region));\n\n        if (regionTarget == null)\n            throw new ArgumentNullException(nameof(regionTarget));\n\n        bool itemsSourceIsSet = regionTarget.Children?.Any() ?? false || regionTarget.IsSet(BindableLayout.ItemsSourceProperty);\n\n        if (itemsSourceIsSet)\n        {\n            throw new InvalidOperationException(Resources.LayoutViewHasChildrenException);\n        }\n\n        BindableLayout.SetItemsSource(regionTarget, region.Views);\n        BindableLayout.SetItemTemplate(regionTarget, new RegionItemsSourceTemplate());\n    }\n\n    /// <summary>\n    /// Creates a new instance of <see cref=\"IRegion\"/>.\n    /// </summary>\n    /// <returns>A new instance of <see cref=\"Region\"/>.</returns>\n    protected override IRegion CreateRegion(IContainerProvider container) =>\n        container.Resolve<Region>();\n}\n","sourceCodeStart":19,"sourceCodeEnd":51,"githubUrl":"https://github.com/PrismLibrary/Prism/blob/358118cd640d9a22ff8cf21c8ad197fa038b7990/src/Maui/Prism.Maui/Navigation/Regions/Adapters/LayoutRegionAdapter.cs#L19-L51","documentation":"LayoutRegionAdapter.Adapt throws when the target Layout already has children or a BindableLayout.ItemsSource set. The adapter drives the layout's children through BindableLayout.ItemsSource = region.Views, which cannot coexist with developer-supplied children or a bound items source.","triggerScenarios":"Registering a region name on a StackLayout/Grid/FlexLayout that already declares XAML children (StackLayout with <Label/> etc.) or sets BindableLayout.ItemsSource in XAML/code.","commonSituations":"Adding prism:RegionManager.RegionName to an existing layout full of static controls; combining a bound layout with region registration while migrating from MVVM lists to Prism regions.","solutions":["Empty the layout (remove XAML children) and clear BindableLayout.ItemsSource before region registration.","Or remove the region name and manage children normally if region behavior is not required.","Add views through the region (region.Add / RequestNavigate) rather than declaring them inline."],"exampleFix":"<!-- before -->\n<StackLayout prism:RegionManager.RegionName=\"ItemsRegion\">\n    <Label Text=\"Static\" />\n</StackLayout>\n\n<!-- after -->\n<StackLayout prism:RegionManager.RegionName=\"ItemsRegion\" />","handlingStrategy":"validation","validationCode":"bool regionOk = !(layout.Children?.Any() ?? false) && !layout.IsSet(BindableLayout.ItemsSourceProperty);\nif (!regionOk) throw new InvalidOperationException(\"Clear children/BindableLayout.ItemsSource before using this layout as a region.\");","typeGuard":null,"tryCatchPattern":"try { regionManager.RegisterRegions(); }\ncatch (InvalidOperationException ex) when (ex.Message.Contains(\"children\"))\n{\n    // empty the layout or remove the region name\n}","preventionTips":["Keep region host layouts child-free in XAML.","Do not set BindableLayout.ItemsSource on region hosts.","Audit layouts for both static children and RegionName attributes."],"tags":["regions","layout","maui","children-conflict"],"backgroundTag":"conflicting-config-options","analyzedSha":"358118cd640d9a22ff8cf21c8ad197fa038b7990","analyzedAt":"2026-09-15T15:00:31.079Z","contentChangedAt":"2026-09-15T15:00:31.079Z","schemaVersion":2},"datasetVersion":"2026-09-16T04:17:20.429Z"}