AvaloniaUI/Avalonia · error · InvalidCastException

KeyFrame value doesnt match property type.

Error message

KeyFrame value doesnt match property type.

What it means

Thrown by AnimatorKeyFrame.GetTypedValue<T>() when Value is non-null but no TypeDescriptor converter can convert it to the requested type T. This means the key frame holds a value whose runtime type is incompatible with the property being animated.

Source

Thrown at src/Avalonia.Base/Animation/AnimatorKeyFrame.cs:74

            }
        }

        [RequiresUnreferencedCode(TrimmingMessages.TypeConversionRequiresUnreferencedCodeMessage)]
        public T GetTypedValue<[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.All)] T>()
        {
            var typeConv = TypeDescriptor.GetConverter(typeof(T));

            if (Value == null)
            {
                throw new ArgumentNullException($"KeyFrame value can't be null.");
            }
            if(Value is T typedValue)
            {
                return typedValue;
            }
            if (!typeConv.CanConvertTo(Value.GetType()))
            {
                throw new InvalidCastException($"KeyFrame value doesnt match property type.");
            }

            return (T)typeConv.ConvertTo(Value, typeof(T))!;
        }

        internal override void BuildDebugDisplay(StringBuilder builder, bool includeContent)
        {
            base.BuildDebugDisplay(builder, includeContent);
            DebugDisplayHelper.AppendOptionalValue(builder, nameof(Property), Property, includeContent);
            DebugDisplayHelper.AppendOptionalValue(builder, nameof(Cue), Cue, includeContent);
            DebugDisplayHelper.AppendOptionalValue(builder, nameof(Value), Value, includeContent);
        }
    }
}

View on GitHub (pinned to 11c5427268)

Solutions

  1. Set the AnimatorKeyFrame.Value to an instance of the exact target type T.
  2. Register/ensure a TypeConverter exists between the value's type and T.
  3. Provide the value via the correct setter chain so the XAML loader coerces it.

Example fix

// before
kf.Value = "1.0"; // animating a double property without conversion

// after
kf.Value = 1.0d;
Defensive patterns

Strategy: type-guard

Validate before calling

if (kf.Value is not T &&
    !TypeDescriptor.GetConverter(typeof(T)).CanConvertTo(kf.Value!.GetType()))
    throw new InvalidCastException($"KeyFrame value {kf.Value} cannot convert to {typeof(T)}.");

Type guard

static bool ValueIsCompatible<T>(object? v) => v is T || TypeDescriptor.GetConverter(typeof(T)).CanConvertTo(v?.GetType() ?? typeof(object));

Prevention

When it happens

Trigger: Calling GetTypedValue<T>() where Value's runtime type is not assignable to T and no TypeConverter bridges the two (e.g. Value is a string but T is a custom struct with no converter).

Common situations: XAML key frame value typo (wrong type literal); animating a property with a value type that lacks a registered TypeConverter; mixing Brush types incorrectly.

Related errors


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