AvaloniaUI/Avalonia · error · FormatException

Invalid Cue string "{value}"

Error message

Invalid Cue string "{value}"

What it means

Thrown by Cue.Parse when the input string cannot be parsed to a number. After stripping an optional trailing '%', the remainder must parse as a double via the supplied culture; otherwise a FormatException is raised.

Source

Thrown at src/Avalonia.Base/Animation/Cue.cs:48

        /// <summary>
        /// Parses a string to a <see cref="Cue"/> object.
        /// </summary>
        public static Cue Parse(string value, CultureInfo? culture)
        {
            string v = value;

            if (value.EndsWith('%'))
            {
                v = v.TrimEnd('%');
            }

            if (double.TryParse(v, NumberStyles.Float, culture, out double res))
            {
                return new Cue(res / 100d);
            }
            else
            {
                throw new FormatException($"Invalid Cue string \"{value}\"");
            }
        }

        /// <summary>
        /// Checks for equality between a <see cref="Cue"/>
        /// and a <see cref="double"/> value.
        /// </summary>
        /// <param name="other"></param>
        /// <returns></returns>
        public bool Equals(double other)
        {
            return CueValue == other;
        }
    }

    public class CueTypeConverter : TypeConverter
    {
        public override bool CanConvertFrom(ITypeDescriptorContext? context, Type sourceType)

View on GitHub (pinned to 11c5427268)

Solutions

  1. Pass CultureInfo.InvariantCulture when the value is machine-generated.
  2. Sanitize the input: trim whitespace, remove unsupported characters.
  3. Validate with double.TryParse before constructing the Cue.

Example fix

// before
var cue = Cue.Parse("0,5", CultureInfo.InvariantCulture); // comma vs dot

// after
var cue = Cue.Parse("0.5", CultureInfo.InvariantCulture);
Defensive patterns

Strategy: validation

Validate before calling

var s = input.Trim().TrimEnd('%');
if (!double.TryParse(s, NumberStyles.Float, CultureInfo.InvariantCulture, out _))
    throw new FormatException($"Invalid Cue string: {input}");

Type guard

static bool IsValidCueString(string s) {
    var v = s.Trim().TrimEnd('%');
    return double.TryParse(v, NumberStyles.Float, CultureInfo.InvariantCulture, out _);
}

Try / catch

try { var cue = Cue.Parse(input, CultureInfo.InvariantCulture); }
catch (FormatException) { /* fall back to a default cue */ }

Prevention

When it happens

Trigger: Calling Cue.Parse(s, culture) where s (minus a trailing '%') is not a valid floating-point number in that culture.

Common situations: Localized culture mismatch (decimal separator '.' vs ','); stray characters/whitespace inside the number; empty string; non-numeric tokens.

Related errors


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