AvaloniaUI/Avalonia · error · ArgumentNullException
Resources
Error message
Resources
What it means
The Styles.Resources property assigns the IResourceDictionary backing this Styles collection. The getter lazily initializes a ResourceDictionary (get => _resources ?? (Resources = new ResourceDictionary())), so a null dictionary is never a valid state. The setter throws ArgumentNullException because a null value would break lazy initialization and leave resource lookups with no dictionary to query.
Source
Thrown at src/Avalonia.Base/Styling/Styles.cs:63
private set
{
if (_owner != value)
{
_owner = value;
OwnerChanged?.Invoke(this, EventArgs.Empty);
}
}
}
/// <summary>
/// Gets or sets a dictionary of style resources.
/// </summary>
public IResourceDictionary Resources
{
get => _resources ?? (Resources = new ResourceDictionary());
set
{
value = value ?? throw new ArgumentNullException(nameof(Resources));
var currentOwner = Owner;
if (currentOwner is not null)
{
_resources?.RemoveOwner(currentOwner);
}
_resources = value;
if (currentOwner is not null)
{
_resources.AddOwner(currentOwner);
}
}
}
bool ICollection<IStyle>.IsReadOnly => false;View on GitHub (pinned to 11c5427268)
Solutions
- To clear resources, assign an empty dictionary: styles.Resources = new ResourceDictionary();
- If a dictionary is already assigned, call styles.Resources.Clear() to remove entries without replacing the dictionary.
- Remove any Resources="{x:Null}" from XAML and ensure bindings that feed Resources never yield null (use a fallback value of an empty ResourceDictionary).
Example fix
// before styles.Resources = null; // after styles.Resources = new ResourceDictionary();
Defensive patterns
Strategy: validation
Validate before calling
// Never assign null; assign an empty dictionary to clear. styles.Resources = newResources ?? new ResourceDictionary();
Type guard
static bool IsValidResourceDictionary(IResourceDictionary? dict) => dict is not null;
Prevention
- To clear resources, call styles.Resources.Clear() or assign new ResourceDictionary().
- Ensure any binding feeding Resources has FallbackValue set to an empty ResourceDictionary.
- Remove Resources="{x:Null}" from XAML.
- Treat Resources as non-nullable in code annotations ([NotNull]) to surface accidental nulls at analysis time.
When it happens
Trigger: Assigning styles.Resources = null directly in C#, or a binding/XAML expression that resolves Resources to null (e.g. Resources="{x:Null}"). Also reachable via code paths that intend to 'clear' resources by nulling them.
Common situations: Attempted resource clearing via null assignment instead of clearing the dictionary contents; data-bound resource dictionaries whose source becomes null; migration of older code that assumed nullable Resources; XAML authored with explicit {x:Null}.
Related errors
AI-assisted analysis of AvaloniaUI/Avalonia@11c5427268 (2026-08-13).
Data as JSON: /api/errors/e7ec0d0980649556.
Report an issue: GitHub.