dotnet/wpf · error · InvalidOperationException

SR.Format(SR.CompatibilityPreferencesSealed…

Error message

SR.Format(SR.CompatibilityPreferencesSealed, "AreInactiveSelectionHighlightBrushKeysSupported", "FrameworkCompatibilityPreferences")

What it means

A FrameworkCompatibilityPreferences static property (here AreInactiveSelectionHighlightBrushKeysSupported) was set after the preferences were sealed. WPF seals these app-wide compatibility flags when the first DependencyObject/window is created (or Seal is called), after which they are immutable to keep behavior consistent for the app lifetime.

Solutions

  1. Move the property assignment to the very start of App startup (e.g. App static constructor or before base Application initialization), before any DependencyObject is created.
  2. Check FrameworkCompatibilityPreferences.GetAreInactiveSelectionHighlightBrushKeysSupportedDefault-style internal seal state or wrap in a sealed-check before setting when possible.
  3. If a third-party library triggers the seal too early, set the preference before referencing that library, or use an Application-level workaround instead.
  4. In tests, ensure each test process sets preferences before WPF initialization or isolate into separate processes.

Example fix

// before (App.xaml.cs)
protected override void OnStartup(StartupEventArgs e)
{
    base.OnStartup(e);
    FrameworkCompatibilityPreferences.AreInactiveSelectionHighlightBrushKeysSupported = true; // throws
}

// after
static App() // runs before any window/DependencyObject is created
{
    FrameworkCompatibilityPreferences.AreInactiveSelectionHighlightBrushKeysSupported = true;
}
protected override void OnStartup(StartupEventArgs e) => base.OnStartup(e);
Defensive patterns

Strategy: validation

Validate before calling

// Set preferences before any WPF initialization, e.g. in [STAThread] Main:
FrameworkCompatibilityPreferences.AreInactiveSelectionHighlightBrushKeysSupported = true;
// then create Application/Window

Try / catch

try { FrameworkCompatibilityPreferences.AreInactiveSelectionHighlightBrushKeysSupported = v; }
catch (InvalidOperationException) { /* sealed: preference must be set at app startup, restart required */ }

Prevention

When it happens

Trigger: Setting a FrameworkCompatibilityPreferences property after the Application has started creating windows/DependencyObjects — e.g. in a Window constructor, Loaded handler, or any code running after startup initialization.

Common situations: Setting compatibility preferences in code-behind instead of App startup (before any window is shown); setting them in a library consumed by an app that already initialized WPF; unit tests where a prior test initialized WPF.

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

Appendix: source

Thrown at src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/FrameworkCompatibilityPreferences.cs:81

#if NETFX && !NETCOREAPP
        private static bool _areInactiveSelectionHighlightBrushKeysSupported = BinaryCompatibility.TargetsAtLeast_Desktop_V4_5 ? true : false;
#elif NETCOREAPP
        private static bool _areInactiveSelectionHighlightBrushKeysSupported = true;
#else
        private static bool _areInactiveSelectionHighlightBrushKeysSupported = true;
#endif

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

                    _areInactiveSelectionHighlightBrushKeysSupported = value;
                }
            }
        }

        internal static bool GetAreInactiveSelectionHighlightBrushKeysSupported()
        {
            Seal();

            return AreInactiveSelectionHighlightBrushKeysSupported;
        }

        #endregion AreInactiveSelectionHighlightBrushKeysSupported

        #region KeepTextBoxDisplaySynchronizedWithTextProperty

View on GitHub (pinned to 81131a70a4)