{"record":{"id":"7e338e8b842458fd","repo":"AvaloniaUI/Avalonia","slug":"duration-value-cannot-be-negative","errorCode":null,"errorMessage":"Duration value cannot be negative.","messagePattern":"Duration value cannot be negative\\.","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"src/Avalonia.Base/Animation/AnimationInstance`1.cs","lineNumber":97,"sourceCode":"            FetchProperties();\n        }\n\n        private void FetchProperties()\n        {\n            _easeFunc = _animation.Easing;\n\n            _speedRatioPrev = _speedRatio;\n            _speedRatio = _animation.SpeedRatio;\n            if (_speedRatio < 0d)\n                throw new InvalidOperationException(\"SpeedRatio value cannot be negative.\");\n\n            _initialDelay = _animation.Delay;\n            if (_initialDelay < TimeSpan.Zero)\n                throw new InvalidOperationException(\"Delay value cannot be negative.\");\n\n            _duration = _animation.Duration;\n            if (_duration < TimeSpan.Zero)\n                throw new InvalidOperationException(\"Duration value cannot be negative.\");\n\n            _iterationDelay = _animation.DelayBetweenIterations;\n            if (_iterationDelay < TimeSpan.Zero)\n                throw new InvalidOperationException(\"DelayBetweenIterations value cannot be negative.\");\n\n            if (_animation.IterationCount.RepeatType == IterationType.Many)\n            {\n                if (_animation.IterationCount.Value > long.MaxValue)\n                    throw new InvalidOperationException(\"IterationCount value cannot be larger than long.MaxValue.\");\n                _iterationCount = (long)_animation.IterationCount.Value;\n            }\n            else\n            {\n                _iterationCount = null;\n            }\n\n            _playbackDirectionPrev = _playbackDirection;\n            _playbackDirection = _animation.PlaybackDirection;","sourceCodeStart":79,"sourceCodeEnd":115,"githubUrl":"https://github.com/AvaloniaUI/Avalonia/blob/11c542726898ae954a1ef668c65ec79ec92ab17d/src/Avalonia.Base/Animation/AnimationInstance`1.cs#L79-L115","documentation":"Thrown by AnimationInstance<T>.FetchProperties (called from the constructor) when the Animation.Duration property is set to a negative TimeSpan. Avalonia clamps/validates timing fields at animation startup because the internal time-accumulator logic cannot represent backwards durations. This guard runs every time an animation instance is constructed and reads the current Duration value.","triggerScenarios":"Constructing/running an Animation whose Duration is set to a negative value (e.g. TimeSpan.FromSeconds(-1)). Fires inside AnimationInstance ctor -> FetchProperties when _animation.Duration < TimeSpan.Zero.","commonSituations":"Computing Duration from a difference of two timestamps where start > end yields a negative span; binding Duration from a model property that is uninitialized (-1 sentinel); deserializing XAML/JSON with a typo'd negative number.","solutions":["Clamp Duration to TimeSpan.Zero before running: animation.Duration = source < TimeSpan.Zero ? TimeSpan.Zero : source.","Fix the upstream computation that produced a negative duration (e.g. swap operand order in a subtraction).","Guard the value at the data source / model layer before assigning it to the Animation."],"exampleFix":"// before\nanimation.Duration = TimeSpan.FromSeconds(endTime - startTime); // negative when endTime < startTime\n\n// after\nanimation.Duration = TimeSpan.FromTicks(Math.Max(0, (endTime - startTime).Ticks));","handlingStrategy":"validation","validationCode":"if (animation.Duration < TimeSpan.Zero)\n    throw new ArgumentOutOfRangeException(nameof(animation.Duration), \"Duration must be non-negative.\");","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Compute durations from absolute timestamp deltas with Math.Max(0, delta.Ticks).","Centralize Animation construction in a factory that validates timing fields.","Unit-test edge cases where source values can be negative."],"tags":["animation","validation","timespan","avalonia"],"backgroundTag":null,"analyzedSha":"11c542726898ae954a1ef668c65ec79ec92ab17d","analyzedAt":"2026-08-13T11:57:40.261Z","schemaVersion":2},"datasetVersion":"2026-08-13T14:17:21.547Z"}