{"record":{"id":"7fc08e2a11c2e8e7","repo":"MaterialDesignInXAML/MaterialDesignInXamlToolkit","slug":"entry-foregroundkey-was-not-of-type-color","errorCode":null,"errorMessage":"Entry {foregroundKey} was not of type Color","messagePattern":"Entry (.+?) was not of type Color","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"src/MaterialDesignColors.Wpf/SwatchesProvider.cs","lineNumber":85,"sourceCode":"\n                    hues.Add(GetHue(resourceDictionary, entry));\n                }\n            }\n            return hues;\n        }\n\n        static Hue GetHue(ResourceDictionary dictionary, DictionaryEntry entry)\n        {\n            if (entry.Value is not Color colour)\n            {\n                throw new InvalidOperationException($\"Entry {entry.Key} was not of type {nameof(Color)}\");\n            }\n            string foregroundKey = entry.Key?.ToString() + \"Foreground\";\n            if (dictionary.OfType<DictionaryEntry>()\n                    .Single(de => string.Equals(de.Key.ToString(), foregroundKey, StringComparison.Ordinal))\n                    .Value is not Color foregroundColour)\n            {\n                throw new InvalidOperationException($\"Entry {foregroundKey} was not of type {nameof(Color)}\");\n            }\n\n            return new Hue(entry.Key?.ToString() ?? \"\", colour, foregroundColour);\n        }\n    }\n\n    private static ResourceDictionary? Read(string? assemblyName, string? path)\n    {\n        if (assemblyName is null || path is null)\n            return null;\n\n        lock (_syncLock)\n        {\n            //NB: Application.LoadComponent is not thread safe\n            return (ResourceDictionary)Application.LoadComponent(new Uri(\n                $\"/{assemblyName};component/{path.Replace(\".baml\", \".xaml\")}\",\n                UriKind.RelativeOrAbsolute));\n        }","sourceCodeStart":67,"sourceCodeEnd":103,"githubUrl":"https://github.com/MaterialDesignInXAML/MaterialDesignInXamlToolkit/blob/98edec3a0b96ef272c587b14bcd67ef5f928a7ed/src/MaterialDesignColors.Wpf/SwatchesProvider.cs#L67-L103","documentation":"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.","triggerScenarios":"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).","commonSituations":"Resource dictionaries that define foregrounds as `SolidColorBrush` rather than `Color`; renamed foreground keys across MaterialDesign versions; partial resource packs lacking the foreground counterpart.","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."],"exampleFix":"// before: foreground key is a brush -> throws\n//   <SolidColorBrush x:Key=\"PrimaryHueMidForeground\" Color=\"White\"/>\n\n// after: foreground key is a Color\n//   <Color x:Key=\"PrimaryHueMidForeground\">#FFFFFF</Color>","handlingStrategy":"type-guard","validationCode":"bool ForegroundKeyIsColor(ResourceDictionary d, string fgKey)\n    => d.OfType<DictionaryEntry>()\n         .Where(de => Equals(de.Key?.ToString(), fgKey))\n         .All(de => de.Value is Color);","typeGuard":"static bool EntryIsColor(DictionaryEntry entry) => entry.Value is Color;","tryCatchPattern":"try { hues.Add(SwatchesProvider.GetHue(dict, entry)); }\ncatch (InvalidOperationException) { /* skip malformed hue */ }","preventionTips":["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."],"tags":["wpf","theming","resources","material-design","data-integrity","color","foreground"],"backgroundTag":null,"analyzedSha":"98edec3a0b96ef272c587b14bcd67ef5f928a7ed","analyzedAt":"2026-08-13T14:31:19.111Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}