{"record":{"id":"46a2a709d14ac704","repo":"MaterialDesignInXAML/MaterialDesignInXamlToolkit","slug":"cannot-get-thememanager-outside-of-a-wpf-applicati","errorCode":null,"errorMessage":"Cannot get ThemeManager outside of a WPF application. Use {nameof(ResourceDictionaryExtensions)}.{nameof(ResourceDictionaryExtensions.GetThemeManager)} on the appropriate resource dictionary instead.","messagePattern":"Cannot get ThemeManager outside of a WPF application\\. Use (.+?)\\.(.+?) on the appropriate resource dictionary instead\\.","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"src/MaterialDesignThemes.Wpf/PaletteHelper.cs","lineNumber":25,"sourceCode":"        if (Application.Current is null)\n            throw new InvalidOperationException($\"Cannot get theme outside of a WPF application. Use {nameof(ResourceDictionaryExtensions)}.{nameof(ResourceDictionaryExtensions.GetTheme)} on the appropriate resource dictionary instead.\");\n        return GetResourceDictionary().GetTheme();\n    }\n\n    public virtual void SetTheme(Theme theme)\n    {\n        if (theme is null) throw new ArgumentNullException(nameof(theme));\n        if (Application.Current is null)\n            throw new InvalidOperationException($\"Cannot set theme outside of a WPF application. Use {nameof(ResourceDictionaryExtensions)}.{nameof(ResourceDictionaryExtensions.SetTheme)} on the appropriate resource dictionary instead.\");\n\n        GetResourceDictionary().SetTheme(theme);\n        RecreateThemeDictionaries();\n    }\n\n    public virtual IThemeManager? GetThemeManager()\n    {\n        if (Application.Current is null)\n            throw new InvalidOperationException($\"Cannot get ThemeManager outside of a WPF application. Use {nameof(ResourceDictionaryExtensions)}.{nameof(ResourceDictionaryExtensions.GetThemeManager)} on the appropriate resource dictionary instead.\");\n        return GetResourceDictionary().GetThemeManager();\n    }\n\n    private static ResourceDictionary GetResourceDictionary()\n        => Application.Current.Resources.MergedDictionaries.FirstOrDefault(x => x is IMaterialDesignThemeDictionary) ??\n            Application.Current.Resources;\n\n    /// <summary>\n    /// Removes and readds resource dictionaries with static resource that will be re-evaluated with new theme brushes.\n    /// This is primarily here for the obsolete theme brushes.\n    /// </summary>\n    private static void RecreateThemeDictionaries()\n    {\n        ResourceDictionary root = Application.Current.Resources;\n        for (int i = 0; i < root.MergedDictionaries.Count; i++)\n        {\n            ResourceDictionary dictionary = root.MergedDictionaries[i];\n            if (dictionary[\"MaterialDesign.Resources.RecreateOnThemeChange\"] is bool recreateOnThemeChange && recreateOnThemeChange)","sourceCodeStart":7,"sourceCodeEnd":43,"githubUrl":"https://github.com/MaterialDesignInXAML/MaterialDesignInXamlToolkit/blob/98edec3a0b96ef272c587b14bcd67ef5f928a7ed/src/MaterialDesignThemes.Wpf/PaletteHelper.cs#L7-L43","documentation":"GetThemeManager returns the IThemeManager stored in the application resources and applies the same Application.Current != null guard as the other PaletteHelper methods. When no WPF application is running it throws InvalidOperationException and points to ResourceDictionaryExtensions.GetThemeManager.","triggerScenarios":"Calling PaletteHelper.GetThemeManager() outside a running WPF application (tests, designer, console, pre-startup).","commonSituations":"Subscribing to theme-change notifications in a unit test or design-time context; a service resolving the manager before the app starts.","solutions":["Call ResourceDictionaryExtensions.GetThemeManager on the relevant ResourceDictionary.","Guard with `if (Application.Current is not null) ...` before calling.","Obtain the manager from the dictionary you configured via SetTheme, which stores the ThemeManager."],"exampleFix":"// before\nvar manager = new PaletteHelper().GetThemeManager();\n// after\nvar manager = myResourceDictionary.GetThemeManager();","handlingStrategy":"type-guard","validationCode":"if (Application.Current is null)\n    return myResourceDictionary.GetThemeManager();\nreturn new PaletteHelper().GetThemeManager();","typeGuard":"Application.Current is not null","tryCatchPattern":null,"preventionTips":["Resolve the ThemeManager from the dictionary configured via SetTheme.","Keep theme-notification subscriptions out of non-WPF code paths."],"tags":["wpf","theming","invalid-operation","design-time","palette-helper"],"backgroundTag":null,"analyzedSha":"98edec3a0b96ef272c587b14bcd67ef5f928a7ed","analyzedAt":"2026-08-13T14:31:19.111Z","schemaVersion":2},"datasetVersion":"2026-08-13T19:17:28.613Z"}