dotnet/maui · warning · NotSupportedException

bool ICollection .Remove(Element item) => throw new…

Error message

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

What it means

Thrown by TemporaryWrapper.Remove(Element). TemporaryWrapper is the read-only IList<Element> adapter behind the obsolete Element.LogicalChildren ReadOnlyCollection. Remove returns bool in the interface contract but the adapter throws NotSupportedException because the inner IReadOnlyList is not mutable.

Solutions

  1. Use RemoveLogicalChild(child) to remove logical children.
  2. Treat LogicalChildren as read-only.
  3. Avoid reflecting into internal Element collections.

Example fix

// before (reflection on internal wrapper)
wrapper.Remove(child); // NotSupportedException
// after
element.RemoveLogicalChild(child);
Defensive patterns

Strategy: validation

Validate before calling

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

Try / catch

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

Prevention

When it happens

Trigger: Obtaining the internal TemporaryWrapper (via reflection/internal access) and calling Remove. The public ReadOnlyCollection would block this first; reaching the adapter directly is required to hit this throw.

Common situations: Reflection-based tooling or designer code attempting to remove children through the backing collection; assuming the wrapper supports removal.

Related errors


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

Appendix: source

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

			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)