AvaloniaUI/Avalonia · error · InvalidDataException

Invalid fill rule

Error message

Invalid fill rule

What it means

Thrown by SetFillRule's inner switch when the single fill-rule character is neither '0' nor '1'. The token was read successfully and is exactly one character long, but that character is something else (a letter, a digit other than 0/1, or punctuation).

Source

Thrown at src/Avalonia.Base/Media/PathMarkupParser.cs:215

            ThrowIfDisposed();

            if (!ReadArgument(ref span, out var fillRule) || fillRule.Length != 1)
            {
                throw new InvalidDataException("Invalid fill rule.");
            }

            FillRule rule;

            switch (fillRule[0])
            {
                case '0':
                    rule = FillRule.EvenOdd;
                    break;
                case '1':
                    rule = FillRule.NonZero;
                    break;
                default:
                    throw new InvalidDataException("Invalid fill rule");
            }

            _geometryContext.SetFillRule(rule);
        }

        private void CloseFigure()
        {
            ThrowIfDisposed();

            if (_isOpen)
            {
                _geometryContext.EndFigure(true);

                if (_beginFigurePoint != null)
                {
                    _currentPoint = _beginFigurePoint.Value;
                    _beginFigurePoint = null;
                }

View on GitHub (pinned to 11c5427268)

Solutions

  1. Use 'F0' (EvenOdd) or 'F1' (NonZero) only.
  2. Double-check there is no stray character between F and the digit (e.g. 'F:0').
  3. If you do not need to change the fill rule, omit the F command.

Example fix

// before
geometry.Parse("F2 M0,0 L10,10 Z");

// after
geometry.Parse("F1 M0,0 L10,10 Z");  // NonZero
Defensive patterns

Strategy: validation

Validate before calling

var fillChar = /* the single char after 'F' */;
if (fillChar is not ('0' or '1')) throw /* or sanitize */;

Type guard

static bool IsValidFillRuleChar(char c) => c is '0' or '1';

Try / catch

try { parser.Parse(data); }
catch (InvalidDataException ex) when (ex.Message == "Invalid fill rule")
{ /* the F token's char was not 0/1 */ }

Prevention

When it happens

Trigger: Path strings like 'F2', 'Fx', 'F ' where the single char is not 0 or 1.

Common situations: Typing 'F2' or using a locale-specific flag; confusing the numeric fill-rule token with the enum name; stray whitespace captured as the token.

Related errors


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