AvaloniaUI/Avalonia · error · NotSupportedException

Dictionary reset not supported

Error message

Dictionary reset not supported

What it means

Thrown as NotSupportedException by the reset callback of ResourceDictionary.MergedDictionaries when the underlying AvaloniaList is reset (cleared wholesale). MergedDictionaries tracks per-item add/remove to propagate owner notifications, but a bulk reset is not supported because it cannot be reconciled incrementally with the owner.

Source

Thrown at src/Avalonia.Base/Controls/ResourceDictionary.cs:73

                {
                    _mergedDictionaries = new AvaloniaList<IResourceProvider>();
                    _mergedDictionaries.ResetBehavior = ResetBehavior.Remove;
                    _mergedDictionaries.ForEachItem(
                        x =>
                        {
                            if (Owner is not null)
                            {
                                x.AddOwner(Owner);
                            }
                        },
                        x =>
                        {
                            if (Owner is not null)
                            {
                                x.RemoveOwner(Owner);
                            }
                        }, 
                        () => throw new NotSupportedException("Dictionary reset not supported"));
                }

                return _mergedDictionaries;
            }
        }

        public IDictionary<ThemeVariant, IThemeVariantProvider> ThemeDictionaries
        {
            get
            {
                if (_themeDictionary == null)
                {
                    _themeDictionary = new AvaloniaDictionary<ThemeVariant, IThemeVariantProvider>(2);
                    _themeDictionary.ForEachItem(
                        (_, x) =>
                        {
                            if (Owner is not null)
                            {

View on GitHub (pinned to 11c5427268)

Solutions

  1. Remove dictionaries individually (RemoveAt / Remove) instead of calling Clear() on MergedDictionaries.
  2. Rebuild by removing each existing entry in a loop, then adding the new ones.
  3. If you need a wholesale swap, replace the entire ResourceDictionary or Styles collection rather than clearing the merged list.

Example fix

// before
resources.MergedDictionaries.Clear(); // throws NotSupportedException
// after
while (resources.MergedDictionaries.Count > 0)
    resources.MergedDictionaries.RemoveAt(resources.MergedDictionaries.Count - 1);
Defensive patterns

Strategy: validation

Validate before calling

// never call Clear(); remove individually
while (resourceDictionary.MergedDictionaries.Count > 0)
    resourceDictionary.MergedDictionaries.RemoveAt(0);

Try / catch

try { resourceDictionary.MergedDictionaries.Clear(); }
catch (NotSupportedException ex) when (ex.Message.Contains("Dictionary reset"))
{ /* fall back to individual removal loop */ }

Prevention

When it happens

Trigger: Calling ResourceDictionary.MergedDictionaries.Clear() or assigning a new collection that triggers the ForEachItem reset callback (the third Action parameter at line 73). Any operation causing AvaloniaList.ForEachItem's reset handler to fire.

Common situations: Code attempts MergedDictionaries.Clear() to rebuild the merged dictionary set at runtime. Reassigning or resetting the collection via reflection or a binding. Theme/Dictionary swap logic that clears instead of removing items one by one.

Related errors


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