MaterialDesignInXAML/MaterialDesignInXamlToolkit · error · InvalidOperationException

Entry {entry.Key} was not of type Color

Error message

Entry {entry.Key} was not of type Color

What it means

`SwatchesProvider.GetHue` reads a `ResourceDictionary` entry representing a hue and asserts its value is a `Color`. If `entry.Value is not Color`, it throws `InvalidOperationException` naming the offending key. This protects the downstream byte casts and `Hue` construction from invalid resource content.

Source

Thrown at src/MaterialDesignColors.Wpf/SwatchesProvider.cs:78

            var hues = new List<Hue>();
            if (resourceDictionary != null)
            {
                foreach (var entry in resourceDictionary.OfType<DictionaryEntry>()
                    .OrderBy(de => de.Key)
                    .Where(de => !(de.Key.ToString() ?? "").EndsWith("Foreground", StringComparison.Ordinal)))
                {

                    hues.Add(GetHue(resourceDictionary, entry));
                }
            }
            return hues;
        }

        static Hue GetHue(ResourceDictionary dictionary, DictionaryEntry entry)
        {
            if (entry.Value is not Color colour)
            {
                throw new InvalidOperationException($"Entry {entry.Key} was not of type {nameof(Color)}");
            }
            string foregroundKey = entry.Key?.ToString() + "Foreground";
            if (dictionary.OfType<DictionaryEntry>()
                    .Single(de => string.Equals(de.Key.ToString(), foregroundKey, StringComparison.Ordinal))
                    .Value is not Color foregroundColour)
            {
                throw new InvalidOperationException($"Entry {foregroundKey} was not of type {nameof(Color)}");
            }

            return new Hue(entry.Key?.ToString() ?? "", colour, foregroundColour);
        }
    }

    private static ResourceDictionary? Read(string? assemblyName, string? path)
    {
        if (assemblyName is null || path is null)
            return null;

View on GitHub (pinned to 98edec3a0b)

Solutions

  1. Ensure swatch resource entries are `Color` values (use `<Color .../>` / `x:Static` on `Color`), not brushes or strings.
  2. Use the official `MaterialDesignColors` resource assemblies that ship with matching versions of MaterialDesignThemes.
  3. Match `MaterialDesignColors` and `MaterialDesignThemes` NuGet versions exactly so resource keys line up.
  4. If loading custom assemblies, validate each `DictionaryEntry.Value is Color` before calling the provider, or catch `InvalidOperationException` around `GetHue`/swatch loading.

Example fix

// before
var swatches = new SwatchesProvider().Swatches; // throws if a hue key is non-Color

// after (defensive assembly selection)
var provider = new SwatchesProvider();
foreach (var sw in provider.Swatches) { /* safe entries only */ }
// and in XAML keep hues as Color:
//   <Color x:Key="PrimaryHueMidBrush">#3F51B5</Color>
Defensive patterns

Strategy: type-guard

Validate before calling

foreach (DictionaryEntry entry in resourceDictionary)
{
    if (entry.Value is not Color) continue; // skip non-Color entries before provider runs
}

Type guard

static bool EntryIsColor(DictionaryEntry entry) => entry.Value is Color;

Try / catch

try { swatches = new SwatchesProvider().Swatches.ToList(); }
catch (InvalidOperationException ex) when (ex.Message.Contains("was not of type Color"))
{
    // log the offending resource assembly and fall back to a known-good one
}

Prevention

When it happens

Trigger: Loading a swatch resource assembly whose `PrimaryHue.*` (or similar) resource key holds a non-`Color` value (a string, a Brush, a malformed token); a corrupted or hand-edited XAML resource dictionary; version mismatch where a resource was renamed/retyped.

Common situations: Custom/older MaterialDesign resource assemblies; merging a third-party resource dictionary that redefines a hue key as a `SolidColorBrush` instead of a `Color`; partial localization packs that override colour keys.

Related errors


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