dotnet/wpf · error · ArgumentException

SR.Animation_ChildMustBeKeyFrame

Error message

SR.Animation_ChildMustBeKeyFrame

What it means

DoubleAnimationUsingKeyFrames.AddChild throws ArgumentException(SR.Animation_ChildMustBeKeyFrame) when something other than a DoubleKeyFrame (or null) is added as a child of the key-frame animation in XAML/object-model composition. Only key frames may be children of a KeyFrameAnimation.

Solutions

  1. Nest only DoubleKeyFrame-derived elements inside DoubleAnimationUsingKeyFrames
  2. Use explicit key-frame elements such as <LinearDoubleKeyFrame Value="10" KeyTime="0:0:1"/> in XAML
  3. In code, call KeyFrames.Add(new DoubleKeyFrame...) instead of AddChild

Example fix

// before (XAML)
<DoubleAnimationUsingKeyFrames>
  <DoubleAnimation To="100" />
</DoubleAnimationUsingKeyFrames>
// after
<DoubleAnimationUsingKeyFrames>
  <LinearDoubleKeyFrame Value="100" KeyTime="0:0:1" />
</DoubleAnimationUsingKeyFrames>
Defensive patterns

Strategy: validation

Validate before calling

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

Type guard

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

Try / catch

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

Prevention

When it happens

Trigger: Adding a child object that is not a DoubleKeyFrame to DoubleAnimationUsingKeyFrames — e.g. via IAddChild.AddChild from parsed XAML like <DoubleAnimationUsingKeyFrames><DoubleAnimation .../></DoubleAnimationUsingKeyFrames>, or calling AddChild directly with a non-keyframe/null object.

Common situations: XAML typos where a plain animation or timeline is nested inside a key-frame animation instead of a DoubleKeyFrame (DiscreteDoubleKeyFrame, LinearDoubleKeyFrame, SplineDoubleKeyFrame, EasingDoubleKeyFrame); programmatic AddChild misuse.

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

Appendix: source

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

            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)