dotnet/wpf · error · ArgumentException

SR.Animation_ChildMustBeKeyFrame

Error message

SR.Animation_ChildMustBeKeyFrame

What it means

Int16AnimationUsingKeyFrames.AddChild throws ArgumentException(SR.Animation_ChildMustBeKeyFrame) when a child that is not an Int16KeyFrame (or is null) is added. Only Int16KeyFrame-derived objects are valid children of this key-frame animation.

Solutions

  1. Nest only Int16KeyFrame-derived elements (DiscreteInt16KeyFrame, LinearInt16KeyFrame, SplineInt16KeyFrame, EasingInt16KeyFrame)
  2. Match key-frame types to the animation's value type
  3. Use KeyFrames.Add in code rather than AddChild

Example fix

// before (XAML)
<Int16AnimationUsingKeyFrames>
  <LinearDoubleKeyFrame Value="10" KeyTime="0:0:1" />
</Int16AnimationUsingKeyFrames>
// after
<Int16AnimationUsingKeyFrames>
  <LinearInt16KeyFrame Value="10" KeyTime="0:0:1" />
</Int16AnimationUsingKeyFrames>
Defensive patterns

Strategy: validation

Validate before calling

if (child == null || child is not Int16KeyFrame) throw new ArgumentException("Child must be an Int16KeyFrame", nameof(child));

Type guard

static bool IsValidKeyFrameChild(object child) => child is Int16KeyFrame;

Try / catch

try { anim.AddChild(o); } catch (ArgumentException ex) { /* wrong child type — use KeyFrames.Add */ }

Prevention

When it happens

Trigger: IAddChild.AddChild called with a non-keyframe object — e.g. nesting an Int16Animation or other timeline inside <Int16AnimationUsingKeyFrames> in XAML, or a null child passed programmatically.

Common situations: XAML mistakes using wrong key-frame type (e.g. DoubleKeyFrame inside an Int16 key-frame animation); code calling AddChild directly with a null or wrong-typed child.

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/c94a144d3d929a8e. Report an issue: GitHub.

Appendix: source

Thrown at src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/Media/Animation/Generated/Int16AnimationUsingKeyFrames.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)
        {
            Int16KeyFrame keyFrame = child as Int16KeyFrame;

            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)