dotnet/wpf · error · ArgumentException

SR.Animation_ChildMustBeKeyFrame

Error message

SR.Animation_ChildMustBeKeyFrame

What it means

CharAnimationUsingKeyFrames.AddChild throws ArgumentException (SR.Animation_ChildMustBeKeyFrame, paramName "child") when the child added through IAddChild is null or not a CharKeyFrame. Only char-typed key frames may be children of this key-frame animation.

Solutions

  1. Add only CharKeyFrame children, e.g. <DiscreteCharKeyFrame Value="a" KeyTime="0:0:1"/>
  2. Use animation.KeyFrames.Add(new DiscreteCharKeyFrame(...)) in code
  3. Use a different animation type if plain values were intended

Example fix

// before (XAML)
<CharAnimationUsingKeyFrames>
  <String>a</String>
</CharAnimationUsingKeyFrames>
// after
<CharAnimationUsingKeyFrames>
  <DiscreteCharKeyFrame Value="a" KeyTime="0:0:1"/>
</CharAnimationUsingKeyFrames>
Defensive patterns

Strategy: validation

Validate before calling

if (child is CharKeyFrame kf) animation.KeyFrames.Add(kf);
else throw new ArgumentException("Child must be a CharKeyFrame", nameof(child));

Type guard

static bool IsKeyFrameChild(object o) => o is CharKeyFrame;

Try / catch

try { adder.AddChild(child); }
catch (ArgumentException ex) when (ex.ParamName == "child") { /* child was not a CharKeyFrame; fix the child element type */ }

Prevention

When it happens

Trigger: XAML IAddChild usage with a non-CharKeyFrame child inside <CharAnimationUsingKeyFrames>, or calling AddChild(null)/AddChild(wrongType).

Common situations: XAML authoring mistakes: wrong key-frame type (e.g. ByteKeyFrame inside a char animation) or nested plain values instead of key frames.

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

Appendix: source

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

            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)