AvaloniaUI/Avalonia · error · FormatException
Invalid unit: {part.ToString()}
Error message
Invalid unit: {part.ToString()} What it means
Thrown by TransformParser.ParseUnit when a unit token does not match any of the supported units. The recognized units are deg, grad, rad, turn, and px (mapped in s_unitMapping). Any other unit string — or a value with an unrecognized suffix — causes this FormatException, which echoes the offending token.
Source
Thrown at src/Avalonia.Base/Media/Transformation/TransformParser.cs:374
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()}");
}
private static TransformFunction ParseTransformFunction(in ReadOnlySpan<char> part)
{
foreach (var (name, transformFunction) in s_functionMapping)
{
if (part.Equals(name.AsSpan(), StringComparison.OrdinalIgnoreCase))
{
return transformFunction;
}
}
return TransformFunction.Invalid;
}
private static double ToRadians(in UnitValue value)
{
return value.Unit switchView on GitHub (pinned to 11c5427268)
Solutions
- Use only supported units: px for lengths, deg/grad/rad/turn for angles.
- Convert unsupported units (em, %, pt) to px before building the transform string.
- Check the echoed '{part}' token in the message to find the offending unit.
Example fix
// before
var t = TransformOperations.Parse("translate(2em, 1em)"); // 'em' unsupported
// after
var t = TransformOperations.Parse("translate(32px, 16px)"); Defensive patterns
Strategy: validation
Validate before calling
static readonly HashSet<string> s_validUnits =
new() { "deg", "grad", "rad", "turn", "px", "" };
static bool HasValidUnits(string transform)
/* check each numeric token's unit suffix against s_validUnits */; Try / catch
try { return TransformOperations.Parse(s); }
catch (FormatException ex) when (ex.Message.Contains("Invalid unit"))
{ /* convert unsupported unit (em, %, pt) to px and retry */ } Prevention
- Restrict transform units to px (length) and deg/grad/rad/turn (angle).
- Convert CSS-only units (em, rem, %, pt) to px before parsing.
- Inspect the offending unit token in the exception message.
When it happens
Trigger: Supplying a value with a unit suffix not in {deg, grad, rad, turn, px}, e.g. '10em', '5pt', '2%', or a bare unknown token. The ParseUnit loop finds no match and throws.
Common situations: Using CSS units Avalonia's transform parser does not support (em, rem, %, pt, vw); typos in unit names ('degress'); assuming browser-CSS unit coverage that the parser does not implement.
Related errors
- Invalid value {value.Value} {unitString} for {function}
- Invalid transform string: '{s}'.
- Too many provided values.
- Invalid format. {function} expects {count} value(s).
- Invalid text trimming string: '{s}'.
AI-assisted analysis of AvaloniaUI/Avalonia@11c5427268 (2026-08-13).
Data as JSON: /api/errors/2d57095b28471680.
Report an issue: GitHub.