{"record":{"id":"8e4fcab8ae61dbdd","repo":"MaterialDesignInXAML/MaterialDesignInXamlToolkit","slug":"entry-entry-key-was-not-of-type-color","errorCode":null,"errorMessage":"Entry {entry.Key} 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":78,"sourceCode":"            var hues = new List<Hue>();\n            if (resourceDictionary != null)\n            {\n                foreach (var entry in resourceDictionary.OfType<DictionaryEntry>()\n                    .OrderBy(de => de.Key)\n                    .Where(de => !(de.Key.ToString() ?? \"\").EndsWith(\"Foreground\", StringComparison.Ordinal)))\n                {\n\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","sourceCodeStart":60,"sourceCodeEnd":96,"githubUrl":"https://github.com/MaterialDesignInXAML/MaterialDesignInXamlToolkit/blob/98edec3a0b96ef272c587b14bcd67ef5f928a7ed/src/MaterialDesignColors.Wpf/SwatchesProvider.cs#L60-L96","documentation":"`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.","triggerScenarios":"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.","commonSituations":"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.","solutions":["Ensure swatch resource entries are `Color` values (use `<Color .../>` / `x:Static` on `Color`), not brushes or strings.","Use the official `MaterialDesignColors` resource assemblies that ship with matching versions of MaterialDesignThemes.","Match `MaterialDesignColors` and `MaterialDesignThemes` NuGet versions exactly so resource keys line up.","If loading custom assemblies, validate each `DictionaryEntry.Value is Color` before calling the provider, or catch `InvalidOperationException` around `GetHue`/swatch loading."],"exampleFix":"// before\nvar swatches = new SwatchesProvider().Swatches; // throws if a hue key is non-Color\n\n// after (defensive assembly selection)\nvar provider = new SwatchesProvider();\nforeach (var sw in provider.Swatches) { /* safe entries only */ }\n// and in XAML keep hues as Color:\n//   <Color x:Key=\"PrimaryHueMidBrush\">#3F51B5</Color>","handlingStrategy":"type-guard","validationCode":"foreach (DictionaryEntry entry in resourceDictionary)\n{\n    if (entry.Value is not Color) continue; // skip non-Color entries before provider runs\n}","typeGuard":"static bool EntryIsColor(DictionaryEntry entry) => entry.Value is Color;","tryCatchPattern":"try { swatches = new SwatchesProvider().Swatches.ToList(); }\ncatch (InvalidOperationException ex) when (ex.Message.Contains(\"was not of type Color\"))\n{\n    // log the offending resource assembly and fall back to a known-good one\n}","preventionTips":["Keep MaterialDesignColors and MaterialDesignThemes NuGet versions aligned.","Define hue resources as Color, not Brush.","Avoid merging third-party dictionaries that override hue keys."],"tags":["wpf","theming","resources","material-design","data-integrity","color"],"backgroundTag":null,"analyzedSha":"98edec3a0b96ef272c587b14bcd67ef5f928a7ed","analyzedAt":"2026-08-13T14:31:19.111Z","schemaVersion":2},"datasetVersion":"2026-08-13T19:17:28.613Z"}