{"record":{"id":"d74f7c97e7f85d7d","repo":"MaterialDesignInXAML/MaterialDesignInXamlToolkit","slug":"cannot-get-theme-outside-of-a-wpf-application-use","errorCode":null,"errorMessage":"Cannot get theme outside of a WPF application. Use {nameof(ResourceDictionaryExtensions)}.{nameof(ResourceDictionaryExtensions.GetTheme)} on the appropriate resource dictionary instead.","messagePattern":"Cannot get 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":8,"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();","sourceCodeStart":1,"sourceCodeEnd":26,"githubUrl":"https://github.com/MaterialDesignInXAML/MaterialDesignInXamlToolkit/blob/98edec3a0b96ef272c587b14bcd67ef5f928a7ed/src/MaterialDesignThemes.Wpf/PaletteHelper.cs#L1-L26","documentation":"PaletteHelper.GetTheme() reads the theme from Application.Current.Resources, so it only works inside a running WPF Application. When Application.Current is null it throws InvalidOperationException and directs you to ResourceDictionaryExtensions.GetTheme, which operates on an explicit ResourceDictionary instead.","triggerScenarios":"Calling new PaletteHelper().GetTheme() (directly or transitively) when Application.Current is null: design-time data contexts, unit tests, console hosts, or code that runs before the WPF Application object is constructed.","commonSituations":"Reading the theme in a designer or xUnit/nunit test; a shared service calling PaletteHelper from a non-WPF process; calling GetTheme during static initialization or before app startup completes.","solutions":["Call ResourceDictionaryExtensions.GetTheme on your ResourceDictionary directly, e.g. myResourceDictionary.GetTheme().","Guard the call: `if (Application.Current is not null) helper.GetTheme();`.","In tests/design-time, construct a ResourceDictionary, merge the MaterialDesign theme, and read from it instead of PaletteHelper."],"exampleFix":"// before\nvar theme = new PaletteHelper().GetTheme();\n// after (works in tests/designer)\nvar theme = myResourceDictionary.GetTheme();","handlingStrategy":"type-guard","validationCode":"if (Application.Current is null)\n    return myResourceDictionary.GetTheme();\nreturn new PaletteHelper().GetTheme();","typeGuard":"Application.Current is not null","tryCatchPattern":null,"preventionTips":["In tests/designer, call ResourceDictionaryExtensions.GetTheme on an explicit dictionary.","Avoid PaletteHelper in shared/non-UI code paths.","Gate theme reads behind an abstraction that works without Application.Current."],"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"}