{"record":{"id":"e2ca03b0ffd48f78","repo":"dotnet/wpf","slug":"sr-timing-repeatbehaviornotrepeatduration","errorCode":null,"errorMessage":"SR.Timing_RepeatBehaviorNotRepeatDuration","messagePattern":"SR\\.Timing_RepeatBehaviorNotRepeatDuration","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/Media/Animation/RepeatBehavior.cs","lineNumber":126,"sourceCode":"            {\n                if (!HasCount)\n                    throw new InvalidOperationException(SR.Format(SR.Timing_RepeatBehaviorNotIterationCount, this));\n\n                return _iterationCount;\n            }\n        }\n\n        /// <summary>\n        /// Returns the repeat duration specified by this RepeatBehavior.\n        /// </summary>\n        /// <value>A TimeSpan representing the repeat duration specified by this RepeatBehavior.</value>\n        /// <exception cref=\"InvalidOperationException\">Thrown if this RepeatBehavior does not represent a repeat duration.</exception>\n        public TimeSpan Duration\n        {\n            get\n            {\n                if (!HasDuration)\n                    throw new InvalidOperationException(SR.Format(SR.Timing_RepeatBehaviorNotRepeatDuration, this));\n\n                return _repeatDuration;\n            }\n        }\n\n        /// <summary>\n        /// Creates and returns a <see cref=\"RepeatBehavior\"/> that indicates that a <see cref=\"Timeline\"/>\n        /// should repeat its simple duration forever.\n        /// </summary>\n        /// <value>A <see cref=\"RepeatBehavior\"/> that indicates that a <see cref=\"Timeline\"/>\n        /// should repeat its simple duration forever.</value>\n        public static RepeatBehavior Forever\n        {\n            get\n            {\n                return new RepeatBehavior(RepeatBehaviorType.Forever);\n            }\n        }","sourceCodeStart":108,"sourceCodeEnd":144,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/Media/Animation/RepeatBehavior.cs#L108-L144","documentation":"RepeatBehavior.Duration returns the stored TimeSpan only when the instance represents a repeat duration (HasDuration). Reading it on an IterationCount-based, Forever, or default RepeatBehavior throws InvalidOperationException with Timing_RepeatBehaviorNotRepeatDuration.","triggerScenarios":"Reading rb.Duration when rb = new RepeatBehavior(3), rb == RepeatBehavior.Forever, or rb == default(RepeatBehavior). Only new RepeatBehavior(TimeSpan) instances support Duration.","commonSituations":"Generic animation inspection code that reads Duration for all repeat behaviors; copying a timeline's RepeatBehavior into a settings object assuming it is duration-based; XAML reload logic.","solutions":["Guard with if (rb.HasDuration) before accessing rb.Duration.","Handle the IterationCount and Forever cases explicitly instead of defaulting to Duration.","If a duration is expected, construct with new RepeatBehavior(TimeSpan) and store the TimeSpan alongside instead of re-reading."],"exampleFix":"// before\nTimeSpan dur = repeatBehavior.Duration;\n// after\nTimeSpan dur = repeatBehavior.HasDuration ? repeatBehavior.Duration : TimeSpan.Zero;","handlingStrategy":"type-guard","validationCode":"TimeSpan? dur = repeatBehavior.HasDuration ? repeatBehavior.Duration : (TimeSpan?)null;","typeGuard":"static bool HasRepeatDuration(RepeatBehavior rb) => rb.HasDuration;\nstatic TimeSpan? SafeDuration(RepeatBehavior rb) => rb.HasDuration ? rb.Duration : (TimeSpan?)null;","tryCatchPattern":"TimeSpan dur;\ntry { dur = repeatBehavior.Duration; }\ncatch (InvalidOperationException) { dur = TimeSpan.Zero; }","preventionTips":["Guard with HasDuration before reading Duration.","Remember default(RepeatBehavior) supports neither Count nor Duration.","Keep the original TimeSpan if you constructed with new RepeatBehavior(ts) instead of re-reading."],"tags":["wpf","animation","invalid-operation","repeat-behavior"],"backgroundTag":"invalid-state-transition","analyzedSha":"81131a70a4c573cd62748a5c36908fc4d662daa9","analyzedAt":"2026-09-14T10:12:48.479Z","contentChangedAt":"2026-09-14T10:12:48.479Z","schemaVersion":2},"datasetVersion":"2026-09-21T21:30:21.729Z"}