AvaloniaUI/Avalonia · error · InvalidOperationException

Invalid animation duration

Error message

Invalid animation duration

What it means

Thrown by the KeyFrameAnimationInstance constructor when duration.Ticks <= 0. Even though the KeyFrameAnimation.Duration setter is supposed to enforce a minimum of 1ms, the instance constructor re-validates defensively; a non-positive duration makes iteration-count math and time interpolation undefined.

Source

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

            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;
            var res = EvaluateImpl(elapsed, currentValue, ref ctx);
            

View on GitHub (pinned to 11c5427268)

Solutions

  1. Ensure Duration is at least TimeSpan.FromMilliseconds(1) before starting the animation.
  2. Guard the duration against <= TimeSpan.Zero and fall back to a sane default.
  3. If maintaining the library, also fix KeyFrameAnimation.Duration to validate the incoming value so this constructor never receives an invalid duration.

Example fix

// before
anim.Duration = computed; // computed is TimeSpan.Zero
visual.StartAnimation("Opacity", anim); // throws in instance ctor
// after
anim.Duration = computed > TimeSpan.Zero ? computed : TimeSpan.FromMilliseconds(250);
Defensive patterns

Strategy: validation

Validate before calling

if (duration <= TimeSpan.Zero) duration = TimeSpan.FromMilliseconds(250);
anim.Duration = duration;

Prevention

When it happens

Trigger: Passing a Duration of TimeSpan.Zero or negative into a composition animation instance. Because of the KeyFrameAnimation.Duration setter bug (it validates the wrong variable), a zero/negative duration can slip through to here.

Common situations: Duration computed from a multiplier that yields zero; relying on the setter to reject zero but hitting the buggy _duration check; constructing a KeyFrameAnimationInstance directly with a default TimeSpan.

Related errors


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