AvaloniaUI/Avalonia · error · FormatException

Invalid transform string: '{s}'.

Error message

Invalid transform string: '{s}'.

What it means

Thrown by TransformParser.Parse (via its local ThrowInvalidFormat function) when the transform string cannot be interpreted as a valid CSS-style transform. After early null/empty and 'none' checks, the parser tokenizes the string into transform functions (e.g. translate, rotate, scale) with their arguments; if the structure is malformed it raises this FormatException. The message echoes the offending input string.

Source

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

            ("skewY", TransformFunction.SkewY),
            ("rotate", TransformFunction.Rotate),
            ("matrix", TransformFunction.Matrix)
        };

        private static readonly (string, Unit)[] s_unitMapping =
        {
            ("deg", Unit.Degree),
            ("grad", Unit.Gradian),
            ("rad", Unit.Radian),
            ("turn", Unit.Turn), 
            ("px", Unit.Pixel)
        };

        public static TransformOperations Parse(string s)
        {
            void ThrowInvalidFormat()
            {
                throw new FormatException($"Invalid transform string: '{s}'.");
            }

            if (string.IsNullOrEmpty(s))
            {
                throw new ArgumentException(nameof(s));
            }

            var span = s.AsSpan().Trim();

            if (span.Equals("none".AsSpan(), StringComparison.OrdinalIgnoreCase))
            {
                return TransformOperations.Identity;
            }

            var builder = TransformOperations.CreateBuilder(0);

            while (true)
            {

View on GitHub (pinned to 11c5427268)

Solutions

  1. Validate the transform string syntax against the supported function set (translate, translateX/Y, rotate, scale, skew, matrix, etc.) before parsing.
  2. Use TransformOperations builder APIs instead of string parsing when constructing transforms programmatically.
  3. Inspect the echoed '{s}' value in the message to locate the exact malformed token.

Example fix

// before
var t = TransformOperations.Parse("transalte(10px,20px)"); // typo, throws

// after
var t = TransformOperations.Parse("translate(10px, 20px)");
Defensive patterns

Strategy: try-catch

Try / catch

try { return TransformOperations.Parse(s); }
catch (FormatException ex) { log.Error($"Bad transform '{s}'", ex); return TransformOperations.Identity; }

Prevention

When it happens

Trigger: Calling TransformOperations.Parse with a malformed transform expression — e.g. an unknown function name, mismatched parentheses, missing arguments, or a stray token the tokenizer cannot classify. The error originates inside the local ThrowInvalidFormat closure invoked from the main parse loop.

Common situations: Loading a transform from CSS/XAML/markup authored by hand with syntax errors; copy-pasting a WPF or browser transform that uses a function Avalonia does not support; concatenating transform fragments dynamically and producing malformed output; version differences where a transform function name changed.

Related errors


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