dotnet/wpf · error · ArgumentException

Animation_ChildMustBeKeyFrame

Animation_ChildMustBeKeyFrame

Error message

SR.Animation_ChildMustBeKeyFrame (Animation_ChildMustBeKeyFrame)

What it means

ByteAnimationUsingKeyFrames.AddChild throws ArgumentException (SR.Animation_ChildMustBeKeyFrame, paramName "child") when a null or non-KeyFrame child is added via the IAddChild AddChild path. Key-frame animations only accept ByteKeyFrame children, so any other child object is rejected.

Solutions

  1. Add only ByteKeyFrame-derived children such as <DiscreteByteKeyFrame Value="10" KeyTime="0:0:1"/>
  2. Use the KeyFrames collection property explicitly in code: animation.KeyFrames.Add(new LinearByteKeyFrame(...))
  3. If a plain value was intended, use ByteAnimation instead of ByteAnimationUsingKeyFrames

Example fix

// before (XAML)
<ByteAnimationUsingKeyFrames>
  <Byte>10</Byte>
</ByteAnimationUsingKeyFrames>
// after
<ByteAnimationUsingKeyFrames>
  <DiscreteByteKeyFrame Value="10" KeyTime="0:0:1"/>
</ByteAnimationUsingKeyFrames>
Defensive patterns

Strategy: validation

Validate before calling

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

Type guard

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

Try / catch

try { adder.AddChild(child); }
catch (ArgumentException ex) when (ex.ParamName == "child") { /* child was not a key frame; fix XAML or wrap in a key frame */ }

Prevention

When it happens

Trigger: Using XAML IAddChild syntax with a child element that is not a ByteKeyFrame (e.g. <ByteAnimationUsingKeyFrames><Byte>...</Byte></ByteAnimationUsingKeyFrames>), or calling AddChild(null) / AddChild(someNonKeyFrame).

Common situations: XAML authoring mistakes: nesting a plain value, DoubleAnimation, or wrong key-frame type inside a key-frame animation; key-frame type doesn't match the animated type.

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

Appendix: source

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

            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)