dotnet/wpf · error · InvalidOperationException

SR.CompatibilityPreferencesSealed

Error message

SR.CompatibilityPreferencesSealed

What it means

CoreCompatibilityPreferences.IsAltKeyRequiredInAccessKeyDefaultScope can only be set before WPF application state is sealed. Once the Application/Dispatcher has been initialized, the setter throws InvalidOperationException because changing this switch afterwards would produce inconsistent behavior.

Solutions

  1. Move the assignment to the entry point before any Application/Dispatcher is created (e.g. before new App() in Main or before App InitializeComponent)
  2. Use [STAThread] Main and set the preference as the first statement
  3. If using generated Main, set the preference in a static constructor/ModuleInitializer that runs before WPF initializes
  4. Remove the runtime assignment and put the switch in an app.config CompatibilityPreferences section if applicable

Example fix

// before
protected override void OnStartup(StartupEventArgs e)
{
    CoreCompatibilityPreferences.IsAltKeyRequiredInAccessKeyDefaultScope = true;
    base.OnStartup(e);
}
// after
[STAThread]
static void Main()
{
    CoreCompatibilityPreferences.IsAltKeyRequiredInAccessKeyDefaultScope = true; // first line
    var app = new App();
    app.InitializeComponent();
    app.Run();
}
Defensive patterns

Strategy: validation

Validate before calling

bool canSetPreference = Application.Current == null; // must run before WPF app state exists

Type guard

null

Try / catch

try
{
    CoreCompatibilityPreferences.IsAltKeyRequiredInAccessKeyDefaultScope = true;
}
catch (InvalidOperationException)
{
    // too late: preference is sealed; log that startup order is wrong
}

Prevention

When it happens

Trigger: Setting CoreCompatibilityPreferences.IsAltKeyRequiredInAccessKeyDefaultScope after an Application, Window, or Dispatcher has been created (the _isSealed flag is set), e.g. from a Window constructor or Loaded event instead of static Main.

Common situations: Developers copying the recommendation into App.xaml.cs constructor, OnStartup, or page code-behind instead of before the Application instance is created; library code touching the switch at runtime.

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/1659a207ff3c5add. Report an issue: GitHub.

Appendix: source

Thrown at src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/CoreCompatibilityPreferences.cs:67

        }

        #endregion CLR compat flags

        #region IsAltKeyRequiredInAccessKeyDefaultScope

        // We decided NOT to opt-in this feature by default.
        private static bool _isAltKeyRequiredInAccessKeyDefaultScope = false;

        public static bool IsAltKeyRequiredInAccessKeyDefaultScope
        {
            get { return _isAltKeyRequiredInAccessKeyDefaultScope; }
            set
            {
                lock (_lockObject)
                {
                    if (_isSealed)
                    {
                        throw new InvalidOperationException(SR.Format(SR.CompatibilityPreferencesSealed, "IsAltKeyRequiredInAccessKeyDefaultScope", "CoreCompatibilityPreferences"));
                    }

                    _isAltKeyRequiredInAccessKeyDefaultScope = value;
                }
            }
        }

        internal static bool GetIsAltKeyRequiredInAccessKeyDefaultScope()
        {
            Seal();

            return IsAltKeyRequiredInAccessKeyDefaultScope;
        }

        #endregion IsAltKeyRequiredInAccessKeyDefaultScope

        #region IncludeAllInkInBoundingBox

View on GitHub (pinned to 81131a70a4)