AvaloniaUI/Avalonia · error · FormatException

If one coordinate is relative, both must be.

Error message

If one coordinate is relative, both must be.

What it means

Thrown by RelativePoint.Parse when exactly one of the X/Y coordinates is specified with a '%' suffix. A RelativePoint is either fully absolute (pixels) or fully relative (0..1 after scaling), so a mixed form like '50%, 100' is ambiguous and rejected as a FormatException.

Source

Thrown at src/Avalonia.Base/RelativePoint.cs:184

        /// Parses a <see cref="RelativePoint"/> string.
        /// </summary>
        /// <param name="s">The string.</param>
        /// <returns>The parsed <see cref="RelativePoint"/>.</returns>
        public static RelativePoint Parse(string s)
        {
            using (var tokenizer = new SpanStringTokenizer(s, CultureInfo.InvariantCulture, exceptionMessage: "Invalid RelativePoint."))
            {
                var x = tokenizer.ReadString();
                var y = tokenizer.ReadString();

                var unit = RelativeUnit.Absolute;
                var scale = 1.0;

                if (x.EndsWith('%'))
                {
                    if (!y.EndsWith('%'))
                    {
                        throw new FormatException("If one coordinate is relative, both must be.");
                    }

                    x = x.TrimEnd('%');
                    y = y.TrimEnd('%');
                    unit = RelativeUnit.Relative;
                    scale = 0.01;
                }

                return new RelativePoint(
                    double.Parse(x, CultureInfo.InvariantCulture) * scale,
                    double.Parse(y, CultureInfo.InvariantCulture) * scale,
                    unit);
            }
        }

        /// <summary>
        /// Returns a String representing this RelativePoint instance.
        /// </summary>

View on GitHub (pinned to 11c5427268)

Solutions

  1. Make both coordinates the same unit: either "50%,50%" or "50,100".
  2. If only one dimension should be relative, use an explicit layout (Grid/Column definitions) rather than a mixed RelativePoint string.
  3. Validate the input string has matching '%' suffixes before parsing.

Example fix

<!-- before -->
<VisualBrush DestinationRect="50%, 100" />
<!-- after -->
<VisualBrush DestinationRect="50%,50%" />
Defensive patterns

Strategy: validation

Validate before calling

if (HasMismatchedPercentSuffix(s)) throw new FormatException("Coordinates must both be absolute or both relative (%).");
RelativePoint.Parse(s);

static bool HasMismatchedPercentSuffix(string s)
{
    var parts = s.Split(',', StringSplitOptions.TrimEntries);
    if (parts.Length != 2) return false;
    return parts[0].EndsWith('%') ^ parts[1].EndsWith('%');
}

Try / catch

try { return RelativePoint.Parse(s); }
catch (FormatException) { /* log/markup error */ throw; }

Prevention

When it happens

Trigger: Parsing a string such as "50%, 100" or "100, 50%" via RelativePoint.Parse (commonly from XAML attribute or style markup). The '%' triggers the relative branch, but the other token lacks '%'.

Common situations: Hand-written XAML/style string with a typo on one coordinate; a binding or converter that appends '%' to only one value; localized input where one unit got stripped.

Related errors


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