MaterialDesignInXAML/MaterialDesignInXamlToolkit · error · InvalidOperationException

Cannot get theme outside of a WPF application. Use

Error message

Cannot get theme outside of a WPF application. Use {nameof(ResourceDictionaryExtensions)}.{nameof(ResourceDictionaryExtensions.GetTheme)} on the appropriate resource dictionary instead.

What it means

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.

Solutions

  1. Call ResourceDictionaryExtensions.GetTheme on your ResourceDictionary directly, e.g. myResourceDictionary.GetTheme().
  2. Guard the call: `if (Application.Current is not null) helper.GetTheme();`.
  3. In tests/design-time, construct a ResourceDictionary, merge the MaterialDesign theme, and read from it instead of PaletteHelper.

Example fix

// before
var theme = new PaletteHelper().GetTheme();
// after (works in tests/designer)
var theme = myResourceDictionary.GetTheme();
Defensive patterns

Strategy: type-guard

Validate before calling

if (Application.Current is null)
    return myResourceDictionary.GetTheme();
return new PaletteHelper().GetTheme();

Type guard

Application.Current is not null

Prevention

When it happens

Trigger: 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.

Common situations: 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.

Related errors


AI-assisted analysis of MaterialDesignInXAML/MaterialDesignInXamlToolkit@98edec3a0b (2026-08-13). Data as JSON: /api/errors/d74f7c97e7f85d7d. Report an issue: GitHub.

Appendix: source

Thrown at src/MaterialDesignThemes.Wpf/PaletteHelper.cs:8

namespace MaterialDesignThemes.Wpf;

public class PaletteHelper
{
    public virtual Theme GetTheme()
    {
        if (Application.Current is null)
            throw new InvalidOperationException($"Cannot get theme outside of a WPF application. Use {nameof(ResourceDictionaryExtensions)}.{nameof(ResourceDictionaryExtensions.GetTheme)} on the appropriate resource dictionary instead.");
        return GetResourceDictionary().GetTheme();
    }

    public virtual void SetTheme(Theme theme)
    {
        if (theme is null) throw new ArgumentNullException(nameof(theme));
        if (Application.Current is null)
            throw new InvalidOperationException($"Cannot set theme outside of a WPF application. Use {nameof(ResourceDictionaryExtensions)}.{nameof(ResourceDictionaryExtensions.SetTheme)} on the appropriate resource dictionary instead.");

        GetResourceDictionary().SetTheme(theme);
        RecreateThemeDictionaries();
    }

    public virtual IThemeManager? GetThemeManager()
    {
        if (Application.Current is null)
            throw new InvalidOperationException($"Cannot get ThemeManager outside of a WPF application. Use {nameof(ResourceDictionaryExtensions)}.{nameof(ResourceDictionaryExtensions.GetThemeManager)} on the appropriate resource dictionary instead.");
        return GetResourceDictionary().GetThemeManager();

View on GitHub (pinned to 98edec3a0b)