AvaloniaUI/Avalonia · error · FormatException

Invalid value {value.Value} {unitString} for {function}

Error message

Invalid value {value.Value} {unitString} for {function}

What it means

Thrown by TransformParser.ThrowFormatInvalidValue when a value supplied to a transform function has a unit that is not valid for that function. The message includes the numeric value, its unit, and the function name, e.g. 'Invalid value 90 deg for translateX'. Different functions accept different units (pixels for translate, degrees/radians for rotate, etc.).

Source

Thrown at src/Avalonia.Base/Media/Transformation/TransformParser.cs:356

            switch (unit)
            {
                case Unit.Radian:
                case Unit.Gradian:
                case Unit.Degree:
                case Unit.Turn:
                {
                    return true;
                }
            }

            return false;
        }

        private static void ThrowFormatInvalidValue(TransformFunction function, in UnitValue value)
        {
            var unitString = value.Unit == Unit.None ? string.Empty : value.Unit.ToString();

            throw new FormatException($"Invalid value {value.Value} {unitString} for {function}");
        }

        private static void ThrowFormatInvalidValueCount(TransformFunction function, int count)
        {
            throw new FormatException($"Invalid format. {function} expects {count} value(s).");
        }

        private static Unit ParseUnit(in ReadOnlySpan<char> part)
        {
            foreach (var (name, unit) in s_unitMapping)
            {
                if (part.Equals(name.AsSpan(), StringComparison.OrdinalIgnoreCase))
                {
                    return unit;
                }
            }

            throw new FormatException($"Invalid unit: {part.ToString()}");

View on GitHub (pinned to 11c5427268)

Solutions

  1. Match the unit to the function: translate/skew expect length (px), rotate expects angle (deg/grad/rad/turn).
  2. Inspect the '{function}' and '{unitString}' in the message to identify the mismatch.
  3. Convert units before building the string if the source data is in a different unit.

Example fix

// before
var t = TransformOperations.Parse("rotate(10px)"); // wrong unit for rotate

// after
var t = TransformOperations.Parse("rotate(10deg)");
Defensive patterns

Strategy: validation

Try / catch

try { return TransformOperations.Parse(s); }
catch (FormatException ex) when (ex.Message.Contains("Invalid value"))
{ /* unit mismatch — convert units and retry */ }

Prevention

When it happens

Trigger: Passing a value with an incompatible unit to a transform function: e.g. rotate(10px) (rotation expects an angle unit, not pixels), or translate(10deg) (translation expects length/pixels). The validator rejects the combination via ThrowFormatInvalidValue.

Common situations: Mixing up angle and length units in hand-written transforms; assuming all functions accept any unit; converting from another CSS engine that is more permissive about units.

Related errors


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