{"record":{"id":"9ccec79b285dd1d5","repo":"MaterialDesignInXAML/MaterialDesignInXamlToolkit","slug":"cannot-set-theme-outside-of-a-wpf-application-use","errorCode":null,"errorMessage":"Cannot set theme outside of a WPF application. Use {nameof(ResourceDictionaryExtensions)}.{nameof(ResourceDictionaryExtensions.SetTheme)} on the appropriate resource dictionary instead.","messagePattern":"Cannot set theme 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":16,"sourceCode":"﻿namespace MaterialDesignThemes.Wpf;\n\npublic class PaletteHelper\n{\n    public virtual Theme GetTheme()\n    {\n        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.","sourceCodeStart":1,"sourceCodeEnd":34,"githubUrl":"https://github.com/MaterialDesignInXAML/MaterialDesignInXamlToolkit/blob/98edec3a0b96ef272c587b14bcd67ef5f928a7ed/src/MaterialDesignThemes.Wpf/PaletteHelper.cs#L1-L34","documentation":"The SetTheme counterpart of the GetTheme guard: SetTheme writes into Application.Current.Resources and cannot run when Application.Current is null. It throws InvalidOperationException pointing to ResourceDictionaryExtensions.SetTheme, which applies the theme to a specific ResourceDictionary.","triggerScenarios":"Calling PaletteHelper.SetTheme from a non-WPF host, before the Application object is constructed, or in a unit test.","commonSituations":"Applying a persisted theme during bootstrap before Application exists; applying a theme in a designer or test harness.","solutions":["Use ResourceDictionaryExtensions.SetTheme on the target ResourceDictionary directly.","Defer PaletteHelper.SetTheme until after Application.Current is set (e.g. in the Startup handler).","In tests, build a ResourceDictionary, merge MaterialDesign resources, and call SetTheme on it."],"exampleFix":"// before\nnew PaletteHelper().SetTheme(theme);\n// after\nmyResourceDictionary.SetTheme(theme);","handlingStrategy":"type-guard","validationCode":"if (Application.Current is null)\n    myResourceDictionary.SetTheme(theme);\nelse\n    new PaletteHelper().SetTheme(theme);","typeGuard":"Application.Current is not null","tryCatchPattern":null,"preventionTips":["Apply themes via ResourceDictionaryExtensions.SetTheme in non-WPF contexts.","Defer PaletteHelper.SetTheme until after the Application object is constructed."],"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"}