dotnet/wpf · error · InvalidOperationException

SR.InputLanguageManager_NotReadyToChangeCurrentLanguage

Error message

SR.InputLanguageManager_NotReadyToChangeCurrentLanguage

What it means

InputLanguageManager.SetSourceCurrentLanguageId throws InvalidOperationException when the manager has no associated PresentationSource yet (_source == null). CurrentInputLanguage can only be forwarded to a source once the manager is attached to one. Called from CurrentInputLanguage and Focus, it means the caller is setting the input language before the element is connected to a presentation source.

Solutions

  1. Defer the call until after the element/window is loaded and shown (e.g. in the Loaded event handler) so the PresentationSource exists.
  2. Check PresentationSource.FromVisual(element) != null before touching CurrentInputLanguage.
  3. If input language must be tracked early, listen for SourceInitialized on the Window and set the language there.

Example fix

// before
InputLanguageManager.CurrentInputLanguage = new CultureInfo("ja-JP"); // in constructor
// after
window.SourceInitialized += (s, e) =>
    InputLanguageManager.CurrentInputLanguage = new CultureInfo("ja-JP");
Defensive patterns

Strategy: validation

Validate before calling

if (PresentationSource.FromVisual(myElement) == null)
    throw new InvalidOperationException("Element not attached to a PresentationSource yet");
InputLanguageManager.CurrentInputLanguage = culture;

Type guard

static bool CanSetInputLanguage(DispatcherObject obj) =>
    PresentationSource.FromDependencyObject(obj as DependencyObject) != null;

Try / catch

try { mgr.CurrentInputLanguage = culture; }
catch (InvalidOperationException) { /* retry on Loaded */ }

Prevention

When it happens

Trigger: Setting InputLanguageManager.CurrentInputLanguage or calling Focus on an element whose InputLanguageManager has not yet been associated with a PresentationSource (e.g. element not yet in a visual tree, or called during construction before the window is shown).

Common situations: Manipulating input language in a window constructor or before the first layout/render pass; headless or off-screen element scenarios where the HwndSource is not yet created.

Understand the failure class

Background: "Invalid state transition" errors: "status must be X, actually Y", "already rejected/charging/uninstalled", "cannot ... while running" — what they mean when a library rejects your call — this error's family across 31 libraries.

Related errors


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

Appendix: source

Thrown at src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/Input/InputLanguageManager.cs:459

                int count = SafeNativeMethods.GetKeyboardLayoutList(0, null);

                return (count > 1);
            }
        }

        #endregion Internal Properties
 
        //------------------------------------------------------
        //
        //  Private Methods
        //
        //------------------------------------------------------

        private void SetSourceCurrentLanguageId(CultureInfo languageId)
        {
            if (_source == null)
            {
                throw new InvalidOperationException(SR.InputLanguageManager_NotReadyToChangeCurrentLanguage);
            }

            _source.CurrentInputLanguage = languageId;
        }

        //------------------------------------------------------
        //
        //  Private Events
        //
        //------------------------------------------------------

        /// <summary>
        ///     This is an event when the input language is changed.
        /// </summary>
        private event InputLanguageEventHandler _InputLanguageChanged;

        /// <summary>
        ///     This is an event when the input language is being changed.

View on GitHub (pinned to 81131a70a4)