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 AvaloniaDictionaryExtensions.ForEachItem when the observed dictionary raises a Reset collection-changed event but the caller did not supply a reset callback. ForEachItem needs a reset handler to know how to rebuild its tracked state after a wholesale clear/reset.

Source

Thrown at src/Avalonia.Base/Collections/AvaloniaDictionaryExtensions.cs:83

                {
                    case NotifyCollectionChangedAction.Add:
                        Add(e.NewItems!);
                        break;

                    case NotifyCollectionChangedAction.Move:
                    case NotifyCollectionChangedAction.Replace:
                        Remove(e.OldItems!);
                        Add(e.NewItems!);
                        break;

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

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

                        reset();
                        Add(collection);
                        break;
                }
            };

            Add(collection);

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

View on GitHub (pinned to 11c5427268)

Solutions

  1. Provide a non-null reset Action that clears your tracked state (it will be followed by Add callbacks for surviving items).
  2. Prevent the source dictionary from being cleared, or unsubscribe before clearing and re-subscribe after.
  3. If reset truly cannot occur, document it; otherwise always pass a reset handler.

Example fix

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

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

Strategy: validation

Validate before calling

if (canReset) collection.ForEachItem(OnAdd, OnRemove, () => tracked.Clear()); else /* avoid observing clearable dictionary */

Try / catch

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

Prevention

When it happens

Trigger: Calling collection.ForEachItem(added, removed, reset: null) (or omitting reset) on a dictionary that later has Clear() called, which raises NotifyCollectionChangedAction.Reset.

Common situations: Tracking a dictionary that can be cleared by the view model; using the 2-action overload which forwards a null reset; a resource dictionary or styles collection being reset during theme reload.

Related errors


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