AvaloniaUI/Avalonia · error · InvalidOperationException

Animation has no key frames

Error message

Animation has no key frames

What it means

Thrown by the KeyFrameAnimationInstance constructor when the keyFrames array is empty. An animation instance cannot evaluate without at least one key frame to interpolate, so construction is treated as a programmer error (InvalidOperationException) rather than a silent no-op.

Source

Thrown at src/Avalonia.Base/Rendering/Composition/Animations/KeyFrameAnimationInstance.cs:51

            AnimationDelayBehavior delayBehavior, TimeSpan delayTime,
            PlaybackDirection direction, TimeSpan duration,
            AnimationIterationBehavior iterationBehavior,
            int iterationCount, AnimationStopBehavior stopBehavior) : base(target, snapshot)
        {
            _interpolator = interpolator;
            _keyFrames = keyFrames;
            _finalValue = finalValue;
            _delayBehavior = delayBehavior;
            _delayTime = delayTime;
            _direction = direction;
            _duration = duration;
            _iterationBehavior = iterationBehavior;
            _iterationCount = iterationCount;
            _stopBehavior = stopBehavior;
            if (_iterationBehavior == AnimationIterationBehavior.Count)
                _totalDuration = delayTime.Add(TimeSpan.FromTicks(iterationCount * _duration.Ticks));
            if (_keyFrames.Length == 0)
                throw new InvalidOperationException("Animation has no key frames");
            if(_duration.Ticks <= 0)
                throw new InvalidOperationException("Invalid animation duration");
        }


        protected override ExpressionVariant EvaluateCore(TimeSpan now, ExpressionVariant currentValue)
        {
            var starting = ExpressionVariant.Create(_startingValue);
            var ctx = new ExpressionEvaluationContext
            {
                Parameters = Parameters,
                Target = TargetObject,
                CurrentValue = currentValue,
                FinalValue = _finalValue ??  starting,
                StartingValue = starting,
                ForeignFunctionInterface = BuiltInExpressionFfi.Instance
            };
            var elapsed = now - _startedAt;

View on GitHub (pinned to 11c5427268)

Solutions

  1. Insert at least one key frame before StartAnimation: anim.InsertKeyFrame(0f, value).
  2. Guard: only start the animation if you have added key frames.
  3. Check a data-driven animation builder returns at least one frame before binding it.

Example fix

// before
var anim = compositor.CreateScalarKeyFrameAnimation();
anim.Duration = TimeSpan.FromMilliseconds(500);
visual.StartAnimation("Opacity", anim); // throws: no key frames
// after
var anim = compositor.CreateScalarKeyFrameAnimation();
anim.Duration = TimeSpan.FromMilliseconds(500);
anim.InsertKeyFrame(0f, 0f);
anim.InsertKeyFrame(1f, 1f);
visual.StartAnimation("Opacity", anim);
Defensive patterns

Strategy: validation

Validate before calling

if (anim has no key frames) { /* add at least one */ anim.InsertKeyFrame(0f, value); }
visual.StartAnimation("Prop", anim);

Prevention

When it happens

Trigger: Starting a KeyFrameAnimation via CompositionObject.StartAnimation before inserting any key frames (no InsertExpressionKeyFrame / InsertKeyFrame calls). The animation's KeyFrames collection is empty when the instance is built.

Common situations: Building an animation dynamically where the key-frame loop added nothing (filtered out, empty data source); copy-paste of an animation definition with the key-frame lines removed; conditional that skipped all InsertKeyFrame calls.

Related errors


AI-assisted analysis of AvaloniaUI/Avalonia@11c5427268 (2026-08-13). Data as JSON: /api/errors/6d4d46d2f09949d1. Report an issue: GitHub.