AvaloniaUI/Avalonia · error · ArgumentException

Name may not be empty

Error message

Name may not be empty

What it means

Thrown by Selectors.Class() when the class name is null, empty, or whitespace. Style class names must be non-empty tokens; a blank name cannot match any class and usually signals a data or markup mistake. Note a null name throws ArgumentNullException first, so this ArgumentException covers empty/whitespace.

Source

Thrown at src/Avalonia.Base/Styling/Selectors.cs:33

        /// <returns>The selector.</returns>
        public static Selector Child(this Selector previous)
        {
            return new ChildSelector(previous);
        }

        /// <summary>
        /// Returns a selector which matches a control's style class.
        /// </summary>
        /// <param name="previous">The previous selector.</param>
        /// <param name="name">The name of the style class.</param>
        /// <returns>The selector.</returns>
        public static Selector Class(this Selector? previous, string name)
        {
            _ = name ?? throw new ArgumentNullException(nameof(name));

            if (string.IsNullOrWhiteSpace(name))
            {
                throw new ArgumentException("Name may not be empty", nameof(name));
            }

            var tac = previous as TypeNameAndClassSelector;

            if (tac != null)
            {
                tac.Classes.Add(name);
                return tac;
            }
            else
            {
                return TypeNameAndClassSelector.ForClass(previous, name);
            }
        }

        /// <summary>
        /// Returns a selector which matches a descendant of a previous selector.
        /// </summary>

View on GitHub (pinned to 11c5427268)

Solutions

  1. Provide a non-empty, trimmed class name.
  2. If the name comes from data, validate/trim it and skip the selector when empty.
  3. Check AXAML for stray dots with no following token.

Example fix

// before
selector = selector.Class(className); // className may be ""

// after
var name = className?.Trim();
if (!string.IsNullOrEmpty(name))
    selector = selector.Class(name);
Defensive patterns

Strategy: validation

Validate before calling

var name = className?.Trim();
if (string.IsNullOrEmpty(name))
    throw new ArgumentException("Class name must be non-empty.", nameof(className));
selector = selector.Class(name);

Type guard

static bool IsValidClassName(string? n) => !string.IsNullOrWhiteSpace(n);

Prevention

When it happens

Trigger: Calling `selector.Class("")`, `selector.Class(" ")`, or passing a variable that resolves to whitespace. In AXAML, a malformed class selector like `. ` or `Button.` with no name.

Common situations: Building class selectors from data that may be blank; trailing/leading whitespace in dynamic class names; AXAML typo where the class token is omitted after the dot.

Related errors


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