dotnet/wpf · error · ArgumentException

SR.Animation_ChildMustBeKeyFrame

Error message

SR.Animation_ChildMustBeKeyFrame

What it means

ObjectAnimationUsingKeyFrames.AddChild throws ArgumentException when the child object is not an ObjectKeyFrame. Key-frame animations only accept key frames as XAML children; any other child type is rejected.

Solutions

  1. Wrap the child in a key frame, e.g. <DiscreteObjectKeyFrame Value="..." KeyTime="...">
  2. Ensure the child derives from ObjectKeyFrame before calling AddChild
  3. Use KeyFrames.Add directly in code with a correctly typed frame

Example fix

// before
<objectAnimationUsingKeyFrames><sys:String>Foo</sys:String></objectAnimationUsingKeyFrames>
// after
<objectAnimationUsingKeyFrames><DiscreteObjectKeyFrame KeyTime="0:0:1" Value="{x:Static sys:String.Empty}"/></objectAnimationUsingKeyFrames>
Defensive patterns

Strategy: validation

Validate before calling

if (child is ObjectKeyFrame kf) animation.AddChild(kf); else throw new ArgumentException("child must be an ObjectKeyFrame");

Type guard

bool IsKeyFrameChild(object c) => c is ObjectKeyFrame;

Try / catch

try { animation.AddChild(child); }
catch (ArgumentException ex) when (ex.ParamName == nameof(child)) { /* wrap child in a DiscreteObjectKeyFrame instead */ }

Prevention

When it happens

Trigger: Calling AddChild(child) with a null or non-ObjectKeyFrame object, or XAML placing an unsupported element inside <ObjectAnimationUsingKeyFrames>.

Common situations: XAML mistakes like nesting a plain object, wrong key-frame type (e.g. a DoubleKeyFrame), or a typo'd element inside the animation.

Understand the failure class

Background: "Must be a positive integer", "Invalid value", "Unsupported": the invalid-argument-value error family, when a library rejects the value you pass — this error's family across 35 libraries.

Related errors


AI-assisted analysis of dotnet/wpf@81131a70a4 (2026-09-14). Data as JSON: /api/errors/1bf93c3c9b221c69. Report an issue: GitHub.

Appendix: source

Thrown at src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/Media/Animation/Generated/ObjectAnimationUsingKeyFrames.cs:227

            WritePostscript();
        }

        /// <summary>
        /// Implemented to allow KeyFrames to be direct children
        /// of KeyFrameAnimations in markup.
        /// </summary>
        [EditorBrowsable(EditorBrowsableState.Advanced)]
        protected virtual void AddChild(object child)
        {
            ObjectKeyFrame keyFrame = child as ObjectKeyFrame;

            if (keyFrame != null)
            {
                KeyFrames.Add(keyFrame);
            }
            else
            {        
                throw new ArgumentException(SR.Animation_ChildMustBeKeyFrame, nameof(child));
            }
        }

        /// <summary>
        /// Adds a text string as a child of this KeyFrameAnimation.
        /// </summary>
        /// <param name="childText">
        /// The text to add.
        /// </param>
        /// <remarks>
        /// A KeyFrameAnimation does not accept text as a child, so this method will
        /// raise an InvalididOperationException unless a derived class has
        /// overridden the behavior to add text.
        /// </remarks>
        /// <exception cref="ArgumentNullException">The childText parameter is
        /// null.</exception>
        void IAddChild.AddText(string childText)
        {

View on GitHub (pinned to 81131a70a4)