{"record":{"id":"1659a207ff3c5add","repo":"dotnet/wpf","slug":"sr-compatibilitypreferencessealed","errorCode":null,"errorMessage":"SR.CompatibilityPreferencesSealed","messagePattern":"SR\\.CompatibilityPreferencesSealed","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/CoreCompatibilityPreferences.cs","lineNumber":67,"sourceCode":"        }\n\n        #endregion CLR compat flags\n\n        #region IsAltKeyRequiredInAccessKeyDefaultScope\n\n        // We decided NOT to opt-in this feature by default.\n        private static bool _isAltKeyRequiredInAccessKeyDefaultScope = false;\n\n        public static bool IsAltKeyRequiredInAccessKeyDefaultScope\n        {\n            get { return _isAltKeyRequiredInAccessKeyDefaultScope; }\n            set\n            {\n                lock (_lockObject)\n                {\n                    if (_isSealed)\n                    {\n                        throw new InvalidOperationException(SR.Format(SR.CompatibilityPreferencesSealed, \"IsAltKeyRequiredInAccessKeyDefaultScope\", \"CoreCompatibilityPreferences\"));\n                    }\n\n                    _isAltKeyRequiredInAccessKeyDefaultScope = value;\n                }\n            }\n        }\n\n        internal static bool GetIsAltKeyRequiredInAccessKeyDefaultScope()\n        {\n            Seal();\n\n            return IsAltKeyRequiredInAccessKeyDefaultScope;\n        }\n\n        #endregion IsAltKeyRequiredInAccessKeyDefaultScope\n\n        #region IncludeAllInkInBoundingBox\n","sourceCodeStart":49,"sourceCodeEnd":85,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/CoreCompatibilityPreferences.cs#L49-L85","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","solutions":["Move the assignment to the entry point before any Application/Dispatcher is created (e.g. before new App() in Main or before App InitializeComponent)","Use [STAThread] Main and set the preference as the first statement","If using generated Main, set the preference in a static constructor/ModuleInitializer that runs before WPF initializes","Remove the runtime assignment and put the switch in an app.config CompatibilityPreferences section if applicable"],"exampleFix":"// before\nprotected override void OnStartup(StartupEventArgs e)\n{\n    CoreCompatibilityPreferences.IsAltKeyRequiredInAccessKeyDefaultScope = true;\n    base.OnStartup(e);\n}\n// after\n[STAThread]\nstatic void Main()\n{\n    CoreCompatibilityPreferences.IsAltKeyRequiredInAccessKeyDefaultScope = true; // first line\n    var app = new App();\n    app.InitializeComponent();\n    app.Run();\n}","handlingStrategy":"validation","validationCode":"bool canSetPreference = Application.Current == null; // must run before WPF app state exists","typeGuard":"null","tryCatchPattern":"try\n{\n    CoreCompatibilityPreferences.IsAltKeyRequiredInAccessKeyDefaultScope = true;\n}\ncatch (InvalidOperationException)\n{\n    // too late: preference is sealed; log that startup order is wrong\n}","preventionTips":["Set all CoreCompatibilityPreferences before creating Application/Window/Dispatcher","Use Main() or ModuleInitializer, never OnStartup or constructors of WPF objects","Never mutate these switches from library code at runtime"],"tags":["wpf","compatibility","configuration","lifecycle"],"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"}