{"record":{"id":"449db5b86cef602f","repo":"dotnet/maui","slug":"nameof-automationid-may-only-be-set-one-time","errorCode":null,"errorMessage":"{nameof(AutomationId)} may only be set one time.","messagePattern":"(.+?) may only be set one time\\.","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"src/Controls/src/Core/Element/Element.cs","lineNumber":88,"sourceCode":"\n\t\tWeakReference<Element> _parentOverride;\n\n\t\tstring _styleId;\n\n\t\tIReadOnlyList<Element> _logicalChildrenReadonly;\n\n\t\tIList<Element> _internalChildren;\n\n\t\t/// <summary>Gets or sets a value that allows the automation framework to find and interact with this element.</summary>\n\t\t/// <value>A value that the automation framework can use to find and interact with this element.</value>\n\t\t/// <remarks>This value may only be set once on an element.</remarks>\n\t\tpublic string AutomationId\n\t\t{\n\t\t\tget { return (string)GetValue(AutomationIdProperty); }\n\t\t\tset\n\t\t\t{\n\t\t\t\tif (AutomationId != null)\n\t\t\t\t\tthrow new InvalidOperationException($\"{nameof(AutomationId)} may only be set one time.\");\n\n\t\t\t\tSetValue(AutomationIdProperty, value);\n\t\t\t}\n\t\t}\n\n\t\t/// <summary>Gets or sets a value used to identify a collection of semantically similar elements.</summary>\n\t\t/// <value>A string that represents the collection the element belongs to.</value>\n\t\t/// <remarks>Use the class id property to collect together elements into semantically similar groups for identification in UI testing and in theme engines.</remarks>\n\t\tpublic string ClassId\n\t\t{\n\t\t\tget => (string)GetValue(ClassIdProperty);\n\t\t\tset => SetValue(ClassIdProperty, value);\n\t\t}\n\n\t\t/// <summary>Gets or sets the styles and properties that will be applied to the element during runtime.</summary>\n\t\t/// <value>A collection containing the different <see cref=\"Effect\"/> to be applied to the element.</value>\n\t\tpublic IList<Effect> Effects\n\t\t{","sourceCodeStart":70,"sourceCodeEnd":106,"githubUrl":"https://github.com/dotnet/maui/blob/f377ff1c5ee04d334d8a925f50c83a6b7afddf03/src/Controls/src/Core/Element/Element.cs#L70-L106","documentation":"Element.AutomationId throws InvalidOperationException if set when it already has a non-null value: the setter checks `if (AutomationId != null)` before assigning. Automation IDs are designed to be immutable once assigned (they back test/automation identity), so a second assignment is rejected.","triggerScenarios":"Setting AutomationId twice on the same Element instance; a binding that re-evaluates and reassigns AutomationId; XAML + code both setting it; Hot Reload re-applying the property.","commonSituations":"DataTemplates reusing elements where code resets the id; setting AutomationId in a parent and again in a child merge; two code paths (InitializeComponent + later code) both assigning; Hot Reload of XAML re-running the setter.","solutions":["Set AutomationId exactly once per element instance.","If reusing element instances, assign the id only when it is null: `if (el.AutomationId is null) el.AutomationId = id;`.","Move the assignment to a single initialization site and remove duplicates."],"exampleFix":"// before\nvar el = new Label { AutomationId = \"a\" };\nel.AutomationId = \"b\"; // throws\n\n// after\nvar el = new Label { AutomationId = \"a\" };\nif (string.IsNullOrEmpty(el.AutomationId)) el.AutomationId = \"b\";","handlingStrategy":"validation","validationCode":"static void SetAutomationIdOnce(Element el, string id)\n{\n    if (el.AutomationId is null) el.AutomationId = id;\n}","typeGuard":"static bool CanSetAutomationId(Element el) => el.AutomationId is null;","tryCatchPattern":null,"preventionTips":["Assign AutomationId at a single initialization site.","Guard reassignment: only set when currently null/empty.","When reusing element instances, reset identity through a fresh instance rather than reassigning."],"tags":["element","automationid","immutable","invalidoperation","testing"],"backgroundTag":null,"analyzedSha":"f377ff1c5ee04d334d8a925f50c83a6b7afddf03","analyzedAt":"2026-08-13T14:26:18.069Z","schemaVersion":2},"datasetVersion":"2026-08-13T19:17:28.613Z"}