dotnet/maui · warning · NotSupportedException

void IList<Element>.Insert(int index, Element item) => throw

Error message

void IList<Element>.Insert(int index, Element item) => throw new NotSupportedException();

What it means

Thrown by TemporaryWrapper.Insert(int, Element). TemporaryWrapper is the read-only IList<Element> adapter behind the obsolete Element.LogicalChildren property. All mutation methods throw NotSupportedException because the inner list is IReadOnlyList and IsReadOnly is true.

Source

Thrown at src/Controls/src/Core/Element/Element.cs:1215

			int ICollection<Element>.Count => _inner.Count;

			bool ICollection<Element>.IsReadOnly => true;

			void ICollection<Element>.Add(Element item) => throw new NotSupportedException();

			void ICollection<Element>.Clear() => throw new NotSupportedException();

			bool ICollection<Element>.Contains(Element item) => _inner.IndexOf(item) != -1;

			void ICollection<Element>.CopyTo(Element[] array, int arrayIndex) => throw new NotSupportedException();

			IEnumerator<Element> IEnumerable<Element>.GetEnumerator() => _inner.GetEnumerator();

			System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator() => _inner.GetEnumerator();

			int IList<Element>.IndexOf(Element item) => _inner.IndexOf(item);

			void IList<Element>.Insert(int index, Element item) => throw new NotSupportedException();

			bool ICollection<Element>.Remove(Element item) => throw new NotSupportedException();

			void IList<Element>.RemoveAt(int index) => throw new NotSupportedException();
		}
	}
}

View on GitHub (pinned to f377ff1c5e)

Solutions

  1. Use InsertLogicalChild(index, element) to add children at a position.
  2. Treat LogicalChildren as read-only.
  3. Do not write to internal Element collections.

Example fix

// before (reflection on internal wrapper)
wrapper.Insert(0, child); // NotSupportedException
// after
element.InsertLogicalChild(0, child);
Defensive patterns

Strategy: validation

Validate before calling

if (element.LogicalChildren.IsReadOnly)
    return; // use InsertLogicalChild instead

Try / catch

// Adapter reached only via reflection; use InsertLogicalChild instead.

Prevention

When it happens

Trigger: Obtaining the internal TemporaryWrapper (via reflection or inside Hot Reload internals) and calling Insert. The public ReadOnlyCollection guards this; reaching Insert requires direct access to the adapter.

Common situations: Reflection-based or designer tooling attempting to insert into the logical-children backing collection; assuming the wrapper supports insertion.

Related errors


AI-assisted analysis of dotnet/maui@f377ff1c5e (2026-08-13). Data as JSON: /api/errors/aa954dabb1399749. Report an issue: GitHub.