dotnet/maui · error · ArgumentNullException

Loop Manager expects a UICollectionViewFlowLayout

Error message

Loop Manager expects a UICollectionViewFlowLayout

What it means

CarouselViewLoopManager constructor throws ArgumentNullException when the UICollectionViewFlowLayout layout parameter is null. The loop manager implements infinite carousel scrolling and requires a valid flow layout to manage section insets, item spacing, and scroll positioning for the loop mechanism.

Source

Thrown at src/Compatibility/Core/src/iOS/CollectionView/CarouselViewController.cs:499

			else
			{
				CollectionView.Hidden = true;
			}
		}
	}

	class CarouselViewLoopManager : IDisposable
	{
		int _indexOffset = 0;
		UICollectionViewFlowLayout _layout;
		const int LoopCount = 3;
		ILoopItemsViewSource _itemsSource;
		bool _disposed;

		public CarouselViewLoopManager(UICollectionViewFlowLayout layout)
		{
			if (layout == null)
				throw new ArgumentNullException(nameof(layout), "LoopManager expects a UICollectionViewFlowLayout");

			_layout = layout;
		}

		public void CenterIfNeeded(UICollectionView collectionView, bool isHorizontal)
		{
			if (isHorizontal)
				CenterHorizontalIfNeeded(collectionView);
			else
				CenterVerticallyIfNeeded(collectionView);
		}

		protected virtual void Dispose(bool disposing)
		{
			if (!_disposed)
			{
				if (disposing)
				{

View on GitHub (pinned to f377ff1c5e)

Solutions

  1. Ensure a non-null UICollectionViewFlowLayout is created before constructing CarouselViewLoopManager
  2. Guard the constructor call: if (layout != null) _loopManager = new CarouselViewLoopManager(layout);
  3. Review CarouselViewController lifecycle to ensure layout exists before loop manager creation
  4. Dispose the loop manager before disposing the layout to prevent race conditions

Example fix

// before
_loopManager = new CarouselViewLoopManager(_layout); // throws if _layout was disposed

// after
if (_layout != null)
    _loopManager = new CarouselViewLoopManager(_layout);
else
    _loopManager = new CarouselViewLoopManager(new UICollectionViewFlowLayout());
Defensive patterns

Strategy: validation

Validate before calling

// Before constructing CarouselViewLoopManager, null-check the layout
if (layout == null)
    throw new ArgumentNullException(nameof(layout), "UICollectionViewFlowLayout required");
var loopManager = new CarouselViewLoopManager(layout);

Prevention

When it happens

Trigger: Passing null to the CarouselViewLoopManager constructor; CarouselViewController creating the loop manager before the layout is initialized; layout disposed or nulled before loop manager construction during carousel teardown/recreation.

Common situations: CarouselView reconfiguration (e.g. changing layout direction) disposes the old layout before the loop manager is recreated; custom CarouselView setup that doesn't provide a flow layout; initialization race in the CarouselViewController lifecycle.

Related errors


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