AvaloniaUI/Avalonia · error · InvalidOperationException

Reset called on collection without reset handler.

Error message

Reset called on collection without reset handler.

What it means

InvalidOperationException thrown by AvaloniaListExtensions.ForEachItem when the observed list raises a Reset collection-changed event but the caller did not supply a reset callback. Without a reset handler the tracker cannot rebuild its index-aware state after a wholesale reset, so it throws.

Source

Thrown at src/Avalonia.Base/Collections/AvaloniaListExtensions.cs:134

                        break;
                    case NotifyCollectionChangedAction.Replace:
                        if (e.OldStartingIndex < 0)
                        {
                            goto case NotifyCollectionChangedAction.Reset;
                        }

                        Remove(e.OldStartingIndex, e.OldItems!);
                        Add(e.NewStartingIndex, e.NewItems!);
                        break;

                    case NotifyCollectionChangedAction.Remove:
                        Remove(e.OldStartingIndex, e.OldItems!);
                        break;

                    case NotifyCollectionChangedAction.Reset:
                        if (reset == null)
                        {
                            throw new InvalidOperationException(
                                "Reset called on collection without reset handler.");
                        }

                        reset();
                        Add(0, (IList)collection);
                        break;
                }
            };

            Add(0, (IList)collection);

            if (weakSubscription)
            {
                return collection.WeakSubscribe(handler);
            }
            else
            {
                collection.CollectionChanged += handler;

View on GitHub (pinned to 11c5427268)

Solutions

  1. Pass a non-null reset Action that clears/reinitializes your tracked state (it is followed by Add callbacks for the remaining items).
  2. Suppress Reset on the source (avoid Clear raising Reset) or unsubscribe before reloading.
  3. Use the overload that accepts (added, removed, reset) explicitly rather than the 2-action variant.

Example fix

// before
list.ForEachItem(OnAdd, OnRemove, reset: null);

// after
list.ForEachItem(OnAdd, OnRemove, () => { tracked.Clear(); });
Defensive patterns

Strategy: validation

Validate before calling

if (canReset) collection.ForEachItem(OnAdd, OnRemove, () => tracked.Clear());

Try / catch

try { list.ForEachItem(OnAdd, OnRemove, ResetHandler); }
catch (InvalidOperationException ex) when (ex.Message.Contains("reset handler")) { /* rebuild index-aware state */ }

Prevention

When it happens

Trigger: Calling collection.ForEachItem(added, removed, reset: null) (or the 2-action overload that forwards null) on a list that later raises Reset (e.g. AvaloniaList.Clear or a Reset notification from an ObservableCollection).

Common situations: Observing a collection that is cleared/reloaded (filter refresh, data reload, theme change); using the simplified ForEachItem overload that omits the reset handler.

Related errors


AI-assisted analysis of AvaloniaUI/Avalonia@11c5427268 (2026-08-13). Data as JSON: /api/errors/692b2b1c7aa75488. Report an issue: GitHub.