{"record":{"id":"de19cb1c9cb37b9f","repo":"AvaloniaUI/Avalonia","slug":"keyframe-value-doesnt-match-property-type","errorCode":null,"errorMessage":"KeyFrame value doesnt match property type.","messagePattern":"KeyFrame value doesnt match property type\\.","errorType":"exception","errorClass":"InvalidCastException","httpStatus":null,"severity":"error","filePath":"src/Avalonia.Base/Animation/AnimatorKeyFrame.cs","lineNumber":74,"sourceCode":"            }\n        }\n\n        [RequiresUnreferencedCode(TrimmingMessages.TypeConversionRequiresUnreferencedCodeMessage)]\n        public T GetTypedValue<[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.All)] T>()\n        {\n            var typeConv = TypeDescriptor.GetConverter(typeof(T));\n\n            if (Value == null)\n            {\n                throw new ArgumentNullException($\"KeyFrame value can't be null.\");\n            }\n            if(Value is T typedValue)\n            {\n                return typedValue;\n            }\n            if (!typeConv.CanConvertTo(Value.GetType()))\n            {\n                throw new InvalidCastException($\"KeyFrame value doesnt match property type.\");\n            }\n\n            return (T)typeConv.ConvertTo(Value, typeof(T))!;\n        }\n\n        internal override void BuildDebugDisplay(StringBuilder builder, bool includeContent)\n        {\n            base.BuildDebugDisplay(builder, includeContent);\n            DebugDisplayHelper.AppendOptionalValue(builder, nameof(Property), Property, includeContent);\n            DebugDisplayHelper.AppendOptionalValue(builder, nameof(Cue), Cue, includeContent);\n            DebugDisplayHelper.AppendOptionalValue(builder, nameof(Value), Value, includeContent);\n        }\n    }\n}\n","sourceCodeStart":56,"sourceCodeEnd":89,"githubUrl":"https://github.com/AvaloniaUI/Avalonia/blob/11c542726898ae954a1ef668c65ec79ec92ab17d/src/Avalonia.Base/Animation/AnimatorKeyFrame.cs#L56-L89","documentation":"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.","triggerScenarios":"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).","commonSituations":"XAML key frame value typo (wrong type literal); animating a property with a value type that lacks a registered TypeConverter; mixing Brush types incorrectly.","solutions":["Set the AnimatorKeyFrame.Value to an instance of the exact target type T.","Register/ensure a TypeConverter exists between the value's type and T.","Provide the value via the correct setter chain so the XAML loader coerces it."],"exampleFix":"// before\nkf.Value = \"1.0\"; // animating a double property without conversion\n\n// after\nkf.Value = 1.0d;","handlingStrategy":"type-guard","validationCode":"if (kf.Value is not T &&\n    !TypeDescriptor.GetConverter(typeof(T)).CanConvertTo(kf.Value!.GetType()))\n    throw new InvalidCastException($\"KeyFrame value {kf.Value} cannot convert to {typeof(T)}.\");","typeGuard":"static bool ValueIsCompatible<T>(object? v) => v is T || TypeDescriptor.GetConverter(typeof(T)).CanConvertTo(v?.GetType() ?? typeof(object));","tryCatchPattern":null,"preventionTips":["Set AnimatorKeyFrame.Value to an instance of the exact target type.","Register TypeConverters for custom value types used in animations.","Verify the value type matches the animated property at authoring time."],"tags":["animation","type-conversion","keyframe","avalonia"],"backgroundTag":null,"analyzedSha":"11c542726898ae954a1ef668c65ec79ec92ab17d","analyzedAt":"2026-08-13T11:57:40.261Z","schemaVersion":2},"datasetVersion":"2026-08-13T14:17:21.547Z"}