dotnet/maui · error · ArgumentOutOfRangeException

indexPath

Error message

indexPath

What it means

ObservableItemsSource wraps an ObservableCollection for an iOS ItemsView and is pinned to a single section (_section). The indexer throws ArgumentOutOfRangeException when indexPath.Section != _section, ensuring callers stay within the assigned section. This protects against multi-section paths hitting a section-bound observable wrapper.

Source

Thrown at src/Compatibility/Core/src/iOS/CollectionView/ObservableItemsSource.cs:94

				{
					return NSIndexPath.Create(_section, n);
				}
			}

			return NSIndexPath.Create(-1, -1);
		}

		public int GroupCount => 1;

		public int ItemCount => Count;

		public object this[NSIndexPath indexPath]
		{
			get
			{
				if (indexPath.Section != _section)
				{
					throw new ArgumentOutOfRangeException(nameof(indexPath));
				}

				return this[(int)indexPath.Item];
			}
		}

		void CollectionChanged(object sender, NotifyCollectionChangedEventArgs args)
		{
			CollectionView.BeginInvokeOnMainThread(() => CollectionChanged(args));
		}

		void CollectionChanged(NotifyCollectionChangedEventArgs args)
		{
			// Force UICollectionView to get the internal accounting straight 
			if (!CollectionView.Hidden)
				CollectionView.NumberOfItemsInSection(_section);

			switch (args.Action)

View on GitHub (pinned to f377ff1c5e)

Solutions

  1. Keep ItemsLayout grouping consistent with the source — flat layout with ObservableCollection, grouped layout with grouped data.
  2. Avoid replacing the ItemsSource during an in-flight batch update; let the existing ObservableItemsSource finish.
  3. Confirm that any NSIndexPath passed to the indexer has Section equal to the section the wrapper was created for.

Example fix

// before
var item = source[indexPath];
// after
if (indexPath.Section == source.Section)
    item = source[indexPath];
Defensive patterns

Strategy: validation

Validate before calling

// ObservableItemsSource is pinned to a single section.
if (indexPath.Section != expectedSection) return;

Type guard

static bool MatchesSection(NSIndexPath p, nint section) => p.Section == section;

Prevention

When it happens

Trigger: Indexing ObservableItemsSource with an NSIndexPath whose Section does not equal the value passed at construction; e.g. a layout/UI update that recomputes sections while the wrapper still owns only its original section.

Common situations: ObservableCollection rebinding while a reload is in flight; mixing grouped layouts with a non-grouped observable source; a section index mismatch after Insert/Delete animations on the underlying collection.

Related errors


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