AvaloniaUI/Avalonia · error · InvalidOperationException
Delay value cannot be negative.
Error message
Delay value cannot be negative.
What it means
Thrown by AnimationInstance.FetchProperties when Animation.Delay is negative. Delay represents the time before an animation's first iteration starts; a negative delay has no defined meaning in the timing model and is rejected during the per-sync property fetch.
Source
Thrown at src/Avalonia.Base/Animation/AnimationInstance`1.cs:93
_shouldPauseOnInvisible = shouldPauseOnInvisible;
_isFirstFrame = true;
_isInFirstInitialDelay = true;
_speedRatio = 1;
FetchProperties();
}
private void FetchProperties()
{
_easeFunc = _animation.Easing;
_speedRatioPrev = _speedRatio;
_speedRatio = _animation.SpeedRatio;
if (_speedRatio < 0d)
throw new InvalidOperationException("SpeedRatio value cannot be negative.");
_initialDelay = _animation.Delay;
if (_initialDelay < TimeSpan.Zero)
throw new InvalidOperationException("Delay value cannot be negative.");
_duration = _animation.Duration;
if (_duration < TimeSpan.Zero)
throw new InvalidOperationException("Duration value cannot be negative.");
_iterationDelay = _animation.DelayBetweenIterations;
if (_iterationDelay < TimeSpan.Zero)
throw new InvalidOperationException("DelayBetweenIterations value cannot be negative.");
if (_animation.IterationCount.RepeatType == IterationType.Many)
{
if (_animation.IterationCount.Value > long.MaxValue)
throw new InvalidOperationException("IterationCount value cannot be larger than long.MaxValue.");
_iterationCount = (long)_animation.IterationCount.Value;
}
else
{
_iterationCount = null;View on GitHub (pinned to 11c5427268)
Solutions
- Clamp Delay to TimeSpan.Zero (or a positive value) before assignment: anim.Delay = TimeSpan.FromMax(TimeSpan.Zero, value).
- Validate the bound source so Delay cannot be negative (slider/resource minimums).
- If you need 'start immediately', use Delay = TimeSpan.Zero rather than a negative value.
- Audit XAML for stray negative Delay literals.
Example fix
// before <Animation Delay="-0:0:0.5" Duration="0:0:1"/> // after <Animation Delay="0:0:0" Duration="0:0:1"/>
Defensive patterns
Strategy: validation
Validate before calling
// clamp Delay to a non-negative value animation.Delay = computedDelay < TimeSpan.Zero ? TimeSpan.Zero : computedDelay;
Type guard
static bool IsValidDelay(TimeSpan v) => v >= TimeSpan.Zero;
Prevention
- Clamp Delay to TimeSpan.Zero or above before assigning.
- Constrain bound sources so Delay cannot be negative.
- Use Delay = TimeSpan.Zero for 'start immediately'.
- Audit XAML for stray negative Delay literals.
When it happens
Trigger: Setting Animation.Delay to a negative TimeSpan (e.g. via XAML Delay="-0:0:1" or binding Delay to a computed negative duration). FetchProperties reads _animation.Delay and throws when it is less than TimeSpan.Zero.
Common situations: Binding Delay to a computed value that can go negative; XAML typos with a leading minus; theme resources that resolve to negative timespans; arithmetic on delays (e.g. delay = startOffset - elapsed) without clamping.
Related errors
- SpeedRatio value cannot be negative.
- No Setter property assigned.
- Unknown PlaybackBehavior value: {_playbackBehavior}
- Duration value cannot be negative.
- DelayBetweenIterations value cannot be negative.
AI-assisted analysis of AvaloniaUI/Avalonia@11c5427268 (2026-08-13).
Data as JSON: /api/errors/02dc882557d19678.
Report an issue: GitHub.