{"record":{"id":"d3dae1708a9401f5","repo":"MaterialDesignInXAML/MaterialDesignInXamlToolkit","slug":"could-not-locate-required-resource-with-key-s-s","errorCode":null,"errorMessage":"Could not locate required resource with key(s) '{string.Join(\", \", keys)}'","messagePattern":"Could not locate required resource with key\\(s\\) '(.+?)'","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"src/MaterialDesignThemes.Wpf/ResourceDictionaryExtensions.cs","lineNumber":119,"sourceCode":"            SecondaryDark = new ColorPair(GetColor(resourceDictionary, \"MaterialDesign.Brush.Secondary.Dark\"),\n                                          GetColor(resourceDictionary, \"MaterialDesign.Brush.Secondary.Dark.Foreground\")),\n        };\n        LoadThemeColors(resourceDictionary, theme);\n        return theme;\n    }\n\n    private static partial void LoadThemeColors(ResourceDictionary resourceDictionary, Theme theme);\n\n    private static Color GetColor(ResourceDictionary resourceDictionary, params string[] keys)\n    {\n        foreach (string key in keys)\n        {\n            if (TryGetColor(resourceDictionary, key, out Color color))\n            {\n                return color;\n            }\n        }\n        throw new InvalidOperationException($\"Could not locate required resource with key(s) '{string.Join(\", \", keys)}'\");\n    }\n\n    private static bool TryGetColor(ResourceDictionary resourceDictionary, string key, out Color color)\n    {\n        if (resourceDictionary[key] is SolidColorBrush brush)\n        {\n            color = brush.Color;\n            return true;\n        }\n        color = default;\n        return false;\n    }\n\n    public static IThemeManager? GetThemeManager(this ResourceDictionary resourceDictionary)\n    {\n        if (resourceDictionary is null) throw new ArgumentNullException(nameof(resourceDictionary));\n        return resourceDictionary[ThemeManagerKey] as IThemeManager;\n    }","sourceCodeStart":101,"sourceCodeEnd":137,"githubUrl":"https://github.com/MaterialDesignInXAML/MaterialDesignInXamlToolkit/blob/98edec3a0b96ef272c587b14bcd67ef5f928a7ed/src/MaterialDesignThemes.Wpf/ResourceDictionaryExtensions.cs#L101-L137","documentation":"When ResourceDictionaryExtensions.GetTheme cannot find a cached Theme under the CurrentThemeKey, it reconstructs one from the MaterialDesign brush resources. GetColor scans the supplied keys for a SolidColorBrush; if none is found it throws InvalidOperationException listing the keys it tried. This means the target ResourceDictionary does not contain the expected MaterialDesign brushes.","triggerScenarios":"Calling resourceDictionary.GetTheme() on a dictionary that has not merged a MaterialDesign theme resource dictionary, or where the brush keys were removed/renamed (version mismatch between MaterialDesignThemes and MaterialDesignColors).","commonSituations":"Forgetting to add <materialDesign:BundledTheme/> or MaterialDesignTheme.xaml in App.xaml; calling GetTheme on a fresh empty ResourceDictionary; upgrading MaterialDesign and a brush key was renamed.","solutions":["Merge the MaterialDesign resource dictionary (BundledTheme / MaterialDesignTheme) into the target dictionary before calling GetTheme.","Call SetTheme(...) first so the CurrentThemeKey is populated and GetTheme returns it directly without needing the brushes.","Ensure MaterialDesignThemes and MaterialDesignColors versions are aligned."],"exampleFix":"<!-- before: empty dictionary -->\n<ResourceDictionary x:Name=\"rd\"/>\nvar t = rd.GetTheme(); // throws\n<!-- after -->\nrd.MergedDictionaries.Add(new BundledTheme());\nrd.SetTheme(Theme.Create(...));\nvar t = rd.GetTheme();","handlingStrategy":"validation","validationCode":"if (!rd.Contains(\"MaterialDesign.Brush.Primary\"))\n    rd.MergedDictionaries.Add(new BundledTheme());\nrd.SetTheme(theme); // populates CurrentThemeKey so GetTheme won't need the brushes\nvar t = rd.GetTheme();","typeGuard":"resourceDictionary.Contains(\"MaterialDesign.Brush.Primary\")","tryCatchPattern":"try { return rd.GetTheme(); }\ncatch (InvalidOperationException) { /* theme not yet applied */ rd.SetTheme(Theme.Create(...)); return rd.GetTheme(); }","preventionTips":["Always merge a MaterialDesign theme dictionary before reading or setting themes.","Call SetTheme once at startup so GetTheme reads the cached Theme.","Keep MaterialDesignThemes and MaterialDesignColors package versions in sync."],"tags":["wpf","theming","resources","invalid-operation"],"backgroundTag":null,"analyzedSha":"98edec3a0b96ef272c587b14bcd67ef5f928a7ed","analyzedAt":"2026-08-13T14:31:19.111Z","schemaVersion":2},"datasetVersion":"2026-08-13T19:17:28.613Z"}