{"record":{"id":"566ff1ac490345f2","repo":"tui-cs/Terminal.Gui","slug":"theme-cannot-be-set-before-configurationmanager-is","errorCode":null,"errorMessage":"Theme cannot be set before ConfigurationManager is initialized.","messagePattern":"Theme cannot be set before ConfigurationManager is initialized\\.","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"Terminal.Gui/Configuration/ThemeManager.cs","lineNumber":226,"sourceCode":"\n            if (ConfigurationManager.Settings is { } && ConfigurationManager.Settings.TryGetValue (\"Theme\", out ConfigProperty? themeCp))\n            {\n                if (themeCp.HasValue)\n                {\n                    return (themeCp.PropertyValue as string)!;\n                }\n\n                return DEFAULT_THEME_NAME;\n            }\n\n            throw new InvalidOperationException (\"Settings is null.\");\n        }\n\n        set\n        {\n            if (!ConfigurationManager.IsInitialized ())\n            {\n                throw new InvalidOperationException (\"Theme cannot be set before ConfigurationManager is initialized.\");\n            }\n\n            if (ConfigurationManager.Settings is null || !ConfigurationManager.Settings.TryGetValue (\"Theme\", out ConfigProperty? themeCp))\n            {\n                throw new InvalidOperationException (\"Settings is null.\");\n            }\n\n            if (themeCp is null || !themeCp.HasValue)\n            {\n                throw new InvalidOperationException (\"Theme has no value.\");\n            }\n\n            if (!ConfigurationManager.Settings.TryGetValue (\"Themes\", out ConfigProperty? themesCp))\n            {\n                throw new InvalidOperationException (\"Settings has no Themes property.\");\n            }\n\n            string previousThemeValue = GetCurrentThemeName ();","sourceCodeStart":208,"sourceCodeEnd":244,"githubUrl":"https://github.com/tui-cs/Terminal.Gui/blob/2e47b11478db083499917f2ad27c34d30efb0df1/Terminal.Gui/Configuration/ThemeManager.cs#L208-L244","documentation":"Thrown by the ThemeManager.Theme setter when ConfigurationManager.IsInitialized() returns false. The Theme backing store lives in ConfigurationManager.Settings[\"Theme\"], which only exists after ConfigurationManager has initialized its Settings scope; setting Theme before that would corrupt or lose the value, so it is rejected.","triggerScenarios":"Assigning ThemeManager.Theme = \"...\" from a static constructor, a module initializer, a PreInit hook, or any code that runs before Application.Create().Init() / ConfigurationManager.Load has executed.","commonSituations":"Calling ThemeManager.Theme in a static field initializer that runs before the app starts; in a library that is loaded but whose app hasn't booted Terminal.Gui yet; ordering bug where theming code runs before configuration load.","solutions":["Move the ThemeManager.Theme assignment to after Application.Create().Init() (or after ConfigurationManager.Load/Initialize) in your startup sequence.","Guard the assignment: 'if (ConfigurationManager.IsInitialized ()) ThemeManager.Theme = name; else defer it.'","Set the theme via config JSON (the \"Theme\" property) instead of in code, so it is applied during initialization."],"exampleFix":"// before - runs in a static ctor / before init\nstatic MyApp () { ThemeManager.Theme = \"Dark\"; }\n\n// after\nIApplication app = Application.Create ().Init ();\nThemeManager.Theme = \"Dark\";\napp.Run<MyWindow> ();","handlingStrategy":"validation","validationCode":"// Guard theme assignment until ConfigurationManager is ready\nif (ConfigurationManager.IsInitialized ())\n    ThemeManager.Theme = themeName;\nelse\n    _deferredTheme = themeName; // apply after Application.Create().Init()","typeGuard":"static bool CanSetTheme () => ConfigurationManager.IsInitialized ();","tryCatchPattern":"try { ThemeManager.Theme = name; }\ncatch (InvalidOperationException ex) when (ex.Message.Contains (\"before ConfigurationManager is initialized\"))\n{ /* defer until init completes */ }","preventionTips":["Set themes after Application.Create().Init(), never in static constructors.","Prefer setting the theme via config JSON (\"Theme\": \"...\") so it applies during init.","Centralize theme selection in one startup method that runs post-init."],"tags":["config","theme","theme-manager","initialization","ordering"],"backgroundTag":null,"analyzedSha":"2e47b11478db083499917f2ad27c34d30efb0df1","analyzedAt":"2026-08-13T19:20:08.826Z","schemaVersion":2},"datasetVersion":"2026-08-14T00:17:13.853Z"}