dotnet/wpf · error · ArgumentOutOfRangeException

SR.Animation_KeyTime_InvalidPercentValue

Error message

SR.Animation_KeyTime_InvalidPercentValue

What it means

KeyTime.FromPercent throws ArgumentOutOfRangeException (SR.Animation_KeyTime_InvalidPercentValue) because a percent KeyTime must be a double between 0.0 and 1.0, where 1.0 equals 100% of the animation duration. Values outside that interval have no meaning for a percentage-based key time.

Solutions

  1. Convert percentage points to a fraction before calling: KeyTime.FromPercent(pct / 100.0).
  2. Clamp the value: Math.Clamp(fraction, 0.0, 1.0).
  3. Validate input ranges at the config/UI layer (slider 0..1 or divide-by-100).
  4. Catch ArgumentOutOfRangeException and fall back to KeyTime.FromPercent(0.5) or a TimeSpan-based key time.

Example fix

// before
var kt = KeyTime.FromPercent(0.75 * 100); // 75.0 -> throws
// after
var kt = KeyTime.FromPercent(0.75); // or KeyTime.FromPercent(pctPoints / 100.0)
Defensive patterns

Strategy: validation

Validate before calling

if (percent < 0.0 || percent > 1.0)
    throw new ArgumentOutOfRangeException(nameof(percent), "Percent key time must be within [0.0, 1.0].");
var kt = KeyTime.FromPercent(percent);

Type guard

static bool IsValidPercent(double p) => double.IsFinite(p) && p >= 0.0 && p <= 1.0;

Try / catch

try { kt = KeyTime.FromPercent(fraction); }
catch (ArgumentOutOfRangeException)
{ kt = KeyTime.FromPercent(Math.Clamp(fraction, 0.0, 1.0)); }

Prevention

When it happens

Trigger: KeyTime.FromPercent(p) where p < 0.0 or p > 1.0 — typically FromPercent(50) intended as 50%, or a negative fraction from a bad calculation.

Common situations: Confusing percent with percentage points (50 vs 0.5); computing fractions with wrong denominators; deserializing config that stores 0-100 scales; porting code from APIs that use 0-100 percents.

Related errors


AI-assisted analysis of dotnet/wpf@81131a70a4 (2026-09-14). Data as JSON: /api/errors/03208d7058909303. Report an issue: GitHub.

Appendix: source

Thrown at src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/Media/Animation/KeyTime.cs:30

    [TypeConverter(typeof(KeyTimeConverter))]
    public struct KeyTime : IEquatable<KeyTime>
    {
        private object _value;
        private KeyTimeType _type;
        
        #region Static Create Methods

        /// <summary>
        /// Creates a KeyTime that represents a Percent value.
        /// </summary>
        /// <param name="percent">
        /// The percent value provided as a double value between 0.0 and 1.0.
        /// </param>
        public static KeyTime FromPercent(double percent)
        {
            if (percent < 0.0 || percent > 1.0)
            {
                throw new ArgumentOutOfRangeException(nameof(percent), SR.Format(SR.Animation_KeyTime_InvalidPercentValue, percent));
            }

            KeyTime keyTime = new KeyTime
            {
                _value = percent,
                _type = KeyTimeType.Percent
            };

            return keyTime;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="timeSpan"></param>
        public static KeyTime FromTimeSpan(TimeSpan timeSpan)
        {
            if (timeSpan < TimeSpan.Zero)

View on GitHub (pinned to 81131a70a4)