AvaloniaUI/Avalonia · error · InvalidOperationException

You can only set either {nameof(KeyTime)} or {nameof(Cue)}.

Error message

You can only set either {nameof(KeyTime)} or {nameof(Cue)}.

What it means

Thrown by the KeyFrame.KeyTime setter when the key frame has already had its Cue set (TimingMode == KeyFrameTimingMode.Cue). A KeyFrame can be timed by exactly one mechanism — an absolute TimeSpan KeyTime or a normalized Cue — and mixing them is disallowed to keep the timeline well-defined.

Source

Thrown at src/Avalonia.Base/Animation/KeyFrame.cs:51

        public AvaloniaList<IAnimationSetter> Setters { get; } = new AvaloniaList<IAnimationSetter>();

        internal KeyFrameTimingMode TimingMode { get; private set; }

        /// <summary>
        /// Gets or sets the key time of this <see cref="KeyFrame"/>.
        /// </summary>
        /// <value>The key time.</value>
        public TimeSpan KeyTime
        {
            get
            {
                return _ktimeSpan;
            }
            set
            {
                if (TimingMode == KeyFrameTimingMode.Cue)
                {
                    throw new InvalidOperationException($"You can only set either {nameof(KeyTime)} or {nameof(Cue)}.");
                }
                TimingMode = KeyFrameTimingMode.TimeSpan;
                _ktimeSpan = value;
            }
        }

        /// <summary>
        /// Gets or sets the cue of this <see cref="KeyFrame"/>.
        /// </summary>
        /// <value>The cue.</value>
        public Cue Cue
        {
            get
            {
                return _kCue;
            }
            set
            {

View on GitHub (pinned to 11c5427268)

Solutions

  1. Use only one timing mechanism per KeyFrame: either KeyTime or Cue, not both.
  2. If you must switch modes, create a fresh KeyFrame instead of reassigning.
  3. Remove the conflicting Cue= attribute from XAML when using KeyTime=.

Example fix

// before
var kf = new KeyFrame { Cue = new Cue(0.5) };
kf.KeyTime = TimeSpan.FromSeconds(1); // throws

// after
var kf = new KeyFrame { KeyTime = TimeSpan.FromSeconds(1) };
Defensive patterns

Strategy: validation

Validate before calling

if (kf.TimingMode == KeyFrameTimingMode.Cue)
    throw new InvalidOperationException("KeyFrame is cue-timed; cannot set KeyTime.");

Prevention

When it happens

Trigger: Assigning KeyFrame.KeyTime after KeyFrame.Cue has already been assigned on the same instance (or after XAML set the cue).

Common situations: XAML key frame using both Cue= and KeyTime= attributes; code-behind that assigns Cue then later sets KeyTime; copy-pasting between cue- and time-based key frames.

Related errors


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