MaterialDesignInXAML/MaterialDesignInXamlToolkit · error · InvalidOperationException
Entry was not of type Color
Error message
Entry {foregroundKey} was not of type Color What it means
The second guard inside `SwatchesProvider.GetHue`: after finding the hue colour, it looks up the matching `*Foreground` key via `.Single(...)` and requires its value to also be a `Color`. If the foreground entry exists but is not a `Color`, it throws `InvalidOperationException` naming the foreground key. Note the `.Single(...)` call also throws (`InvalidOperationException`/`ArgumentNullException`) if the foreground key is missing entirely.
Solutions
- Provide a matching `*Foreground` key typed as `Color` for every hue key in the resource dictionary.
- Use the bundled `MaterialDesignColors` assemblies whose keys are paired correctly, and keep NuGet versions aligned.
- If authoring custom hues, define both `<Color x:Key="MyHue">` and `<Color x:Key="MyHueForeground">`.
- Wrap swatch loading in try/catch around `InvalidOperationException` to skip malformed swatches.
Example fix
// before: foreground key is a brush -> throws // <SolidColorBrush x:Key="PrimaryHueMidForeground" Color="White"/> // after: foreground key is a Color // <Color x:Key="PrimaryHueMidForeground">#FFFFFF</Color>
Defensive patterns
Strategy: type-guard
Validate before calling
bool ForegroundKeyIsColor(ResourceDictionary d, string fgKey)
=> d.OfType<DictionaryEntry>()
.Where(de => Equals(de.Key?.ToString(), fgKey))
.All(de => de.Value is Color); Type guard
static bool EntryIsColor(DictionaryEntry entry) => entry.Value is Color;
Try / catch
try { hues.Add(SwatchesProvider.GetHue(dict, entry)); }
catch (InvalidOperationException) { /* skip malformed hue */ } Prevention
- Provide a paired *Foreground Color key for every hue key.
- Use the bundled MaterialDesignColors assemblies.
- Validate resource dictionaries contain Color values for hue and foreground keys.
When it happens
Trigger: A hue resource has a valid `Color` but its companion `*Foreground` key is typed as a Brush/string, is missing, or is duplicated (which makes `.Single` throw).
Common situations: Resource dictionaries that define foregrounds as `SolidColorBrush` rather than `Color`; renamed foreground keys across MaterialDesign versions; partial resource packs lacking the foreground counterpart.
Related errors
- Entry was not of type Color
- ColorReference does not contain any color
- Could not locate required resource with key(s)
- Non primary hues provided.
- Cannot get theme outside of a WPF application. Use
AI-assisted analysis of MaterialDesignInXAML/MaterialDesignInXamlToolkit@98edec3a0b (2026-08-13).
Data as JSON: /api/errors/7fc08e2a11c2e8e7.
Report an issue: GitHub.
Appendix: source
Thrown at src/MaterialDesignColors.Wpf/SwatchesProvider.cs:85
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;
lock (_syncLock)
{
//NB: Application.LoadComponent is not thread safe
return (ResourceDictionary)Application.LoadComponent(new Uri(
$"/{assemblyName};component/{path.Replace(".baml", ".xaml")}",
UriKind.RelativeOrAbsolute));
}View on GitHub (pinned to 98edec3a0b)