MaterialDesignInXAML/MaterialDesignInXamlToolkit · error · InvalidOperationException

Cannot set theme outside of a WPF application. Use {nameof(R

Error message

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

What it means

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.

Source

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

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();
    }

    private static ResourceDictionary GetResourceDictionary()
        => Application.Current.Resources.MergedDictionaries.FirstOrDefault(x => x is IMaterialDesignThemeDictionary) ??
            Application.Current.Resources;

    /// <summary>
    /// Removes and readds resource dictionaries with static resource that will be re-evaluated with new theme brushes.

View on GitHub (pinned to 98edec3a0b)

Solutions

  1. Use ResourceDictionaryExtensions.SetTheme on the target ResourceDictionary directly.
  2. Defer PaletteHelper.SetTheme until after Application.Current is set (e.g. in the Startup handler).
  3. In tests, build a ResourceDictionary, merge MaterialDesign resources, and call SetTheme on it.

Example fix

// before
new PaletteHelper().SetTheme(theme);
// after
myResourceDictionary.SetTheme(theme);
Defensive patterns

Strategy: type-guard

Validate before calling

if (Application.Current is null)
    myResourceDictionary.SetTheme(theme);
else
    new PaletteHelper().SetTheme(theme);

Type guard

Application.Current is not null

Prevention

When it happens

Trigger: Calling PaletteHelper.SetTheme from a non-WPF host, before the Application object is constructed, or in a unit test.

Common situations: Applying a persisted theme during bootstrap before Application exists; applying a theme in a designer or test harness.

Related errors


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