{"record":{"id":"c542dd41e21ef8e5","repo":"dotnet/maui","slug":"isheadless-cannot-be-modified-when-the-view-is-ren","errorCode":null,"errorMessage":"IsHeadless cannot be modified when the view is rendered","messagePattern":"IsHeadless cannot be modified when the view is rendered","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"warning","filePath":"src/Controls/src/Core/CompressedLayout.cs","lineNumber":39,"sourceCode":"\t\t/// <returns><see langword=\"true\" /> if layout compression is enabled for <paramref name=\"bindable\" />. Otherwise, returns <see langword=\"false\" />.</returns>\n\t\t[Obsolete(\"CompressedLayout does not provide meaningful functionality and may be removed in a future release. Please remove usage of this API.\")]\n\t\tpublic static bool GetIsHeadless(BindableObject bindable)\n\t\t\t=> (bool)bindable.GetValue(IsHeadlessProperty);\n\n\t\t/// <summary>Turns layout compression on or off for the specified bindable object.</summary>\n\t\t/// <param name=\"bindable\">The <see cref=\"BindableObject\" /> on which to enable or disable layout compression</param>\n\t\t/// <param name=\"value\">The new layout compression value. <see langword=\"true\" /> to enable layout compression</param>\n\t\t[Obsolete(\"CompressedLayout does not provide meaningful functionality and may be removed in a future release. Please remove usage of this API.\")]\n\t\tpublic static void SetIsHeadless(BindableObject bindable, bool value)\n\t\t\t=> bindable.SetValue(IsHeadlessProperty, BooleanBoxes.Box(value));\n\n\t\tstatic void OnIsHeadlessPropertyChanged(BindableObject bindable, object oldValue, object newValue)\n\t\t{\n\t\t\tvar ve = bindable as IVisualElementController;\n\t\t\tif (ve == null)\n\t\t\t\treturn;\n\t\t\tif (ve.IsPlatformEnabled)\n\t\t\t\tthrow new InvalidOperationException(\"IsHeadless cannot be modified when the view is rendered\");\n\t\t}\n\n\t\tstatic readonly BindablePropertyKey HeadlessOffsetPropertyKey =\n\t\t\tBindableProperty.CreateReadOnly(\"HeadlessOffset\", typeof(Point), typeof(CompressedLayout), default(Point));\n\n\t\t/// <summary>Bindable property for <c>HeadlessOffset</c>.</summary>\n\t\t[EditorBrowsable(EditorBrowsableState.Never)]\n\t\t[Obsolete(\"CompressedLayout does not provide meaningful functionality and may be removed in a future release. Please remove usage of this API.\")]\n\t\tpublic static readonly BindableProperty HeadlessOffsetProperty = HeadlessOffsetPropertyKey.BindableProperty;\n\n\t\t/// <summary>For internal use by the Microsoft.Maui.Controls platform.</summary>\n\t\t/// <param name=\"bindable\">For internal use by the Microsoft.Maui.Controls platform.</param>\n\t\t/// <returns>For internal use by the Microsoft.Maui.Controls platform.</returns>\n\t\t[EditorBrowsable(EditorBrowsableState.Never)]\n\t\t[Obsolete(\"CompressedLayout does not provide meaningful functionality and may be removed in a future release. Please remove usage of this API.\")]\n\t\tpublic static Point GetHeadlessOffset(BindableObject bindable)\n\t\t\t=> (Point)bindable.GetValue(HeadlessOffsetProperty);\n","sourceCodeStart":21,"sourceCodeEnd":57,"githubUrl":"https://github.com/dotnet/maui/blob/f377ff1c5ee04d334d8a925f50c83a6b7afddf03/src/Controls/src/Core/CompressedLayout.cs#L21-L57","documentation":"Thrown by CompressedLayout.OnIsHeadlessPropertyChanged when IsHeadless is changed after the view has already been rendered (IsPlatformEnabled is true). IsHeadless controls layout compression — when true, the layout skips creating a platform view for the element. Changing this after the platform view exists would leave the view in an inconsistent state, so the property change is rejected. Note: the entire CompressedLayout API is marked Obsolete as it provides no meaningful functionality in MAUI.","triggerScenarios":"At line 39: `if (ve.IsPlatformEnabled) throw new InvalidOperationException(\"IsHeadless cannot be modified when the view is rendered\")`. The property changed callback fires when SetIsHeadless is called. If the target BindableObject is a VisualElement that has already been rendered (IsPlatformEnabled == true), the throw prevents toggling compression post-render. Triggered by calling CompressedLayout.SetIsHeadless on a view that is already displayed.","commonSituations":"1) Calling CompressedLayout.SetIsHeadless(view, true) in a page lifecycle method after the page is already shown (e.g., OnAppearing). 2) Dynamically toggling compression on a view that's part of a rendered layout. 3) Code ported from Xamarin.Forms where IsHeadless was set at different lifecycle stages. 4) Setting IsHeadless in a binding/value converter that evaluates after render. 5) Not realizing the API is obsolete and attempting runtime changes.","solutions":["Set CompressedLayout.SetIsHeadless before the view is rendered — ideally in the constructor or XAML, before the page appears.","Best option: remove CompressedLayout usage entirely since the API is marked Obsolete and provides no meaningful functionality in MAUI.","If compression-like behavior is needed, use alternative approaches (e.g., IsVisible, layout optimization) instead of the deprecated API.","Never call SetIsHeadless in lifecycle methods that fire after rendering (OnAppearing, OnSizeAllocated, event handlers)."],"exampleFix":"// before\npublic MyPage()\n{\n    InitializeComponent();\n}\nprotected override void OnAppearing()\n{\n    base.OnAppearing();\n    CompressedLayout.SetIsHeadless(myStackLayout, true); // throws — view is rendered\n}\n\n// after — set before render, or remove usage entirely\npublic MyPage()\n{\n    InitializeComponent();\n    CompressedLayout.SetIsHeadless(myStackLayout, true); // before render — OK\n}\n\n// BEST: remove obsolete API entirely (it has no effect in MAUI)\n// Simply delete the SetIsHeadless call.","handlingStrategy":"validation","validationCode":"// Check IsPlatformEnabled before setting IsHeadless\nif ((view as IVisualElementController)?.IsPlatformEnabled == true)\n    return; // too late — skip or log warning\nCompressedLayout.SetIsHeadless(view, value);","typeGuard":"public static bool CanSetIsHeadless(VisualElement view)\n    => view != null && !((IVisualElementController)view).IsPlatformEnabled;","tryCatchPattern":null,"preventionTips":["Set CompressedLayout.SetIsHeadless before the view renders — in constructors or XAML only.","Best practice: remove CompressedLayout usage entirely (API is obsolete in MAUI).","Never call SetIsHeadless in OnAppearing or post-render lifecycle methods."],"tags":["maui","layout","obsolete","lifecycle","compressed-layout"],"backgroundTag":null,"analyzedSha":"f377ff1c5ee04d334d8a925f50c83a6b7afddf03","analyzedAt":"2026-08-13T14:26:18.069Z","schemaVersion":2},"datasetVersion":"2026-08-13T19:17:28.613Z"}