{"record":{"id":"23a92f3fbaf7fe81","repo":"dotnet/wpf","slug":"sr-keycollectionhasinvalidkey","errorCode":null,"errorMessage":"SR.KeyCollectionHasInvalidKey","messagePattern":"SR\\.KeyCollectionHasInvalidKey","errorType":"exception","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/ResourceDictionary.cs","lineNumber":1296,"sourceCode":"                    if (keyRecord != null && keyRecord.HasStaticResources)\n                    {\n                        SetOptimizedStaticResources(keyRecord.StaticResources, serviceProvider, staticResourceWorker);\n                    }\n\n                    _baseDictionary.Add(value, keyRecord);\n\n                    if (TraceResourceDictionary.IsEnabled)\n                    {\n                        TraceResourceDictionary.TraceActivityItem(\n                            TraceResourceDictionary.SetKey,\n                            this,\n                            value);\n                    }\n\n                }\n                else\n                {\n                    throw new ArgumentException(SR.KeyCollectionHasInvalidKey);\n                }\n            }\n\n            // Notify owners of the HasImplicitStyles flag value\n            // but there is not need to fire an invalidation.\n            NotifyOwners(new ResourcesChangeInfo(null, this));\n        }\n\n        /// <summary>\n        /// Convert the OptimizedStaticResource and StaticResource items into StaticResourceHolders.\n        /// A StaticResourceHolder is derived from StaticResourceExtension and is a MarkupExtension.\n        /// The differences is that it contain a DeferredResourceReference as its \"PrefetchedValue\".\n        /// DeferredResourceReferences hold the dictionary and the key of the resource.  It is a\n        /// way of looking up the reference now (for later use) but not expanding the entry.\n        /// Also the dictionary has a reference back to the Deferrred Reference.  If dictionary entry\n        /// is modifed the DeferredResourceReference is told and it will grab the old value.\n        /// StaticResourceHolder is a MarkupExtension and thus can be returned as a \"Value\" in the Node Stream.\n        ///","sourceCodeStart":1278,"sourceCodeEnd":1314,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/ResourceDictionary.cs#L1278-L1314","documentation":"Thrown as ArgumentException from ResourceDictionary.SetKeys when a KeyRecord in the deferred-content key collection has neither a KeyString nor a valid type — i.e. the key is invalid. This is an internal invariant of the BAML deferred-content parser: every key record must carry a usable key.","triggerScenarios":"Deferred BAML content whose key collection contains a KeyRecord with null KeyString and no recognized key type (e.g. corrupted or hand-built key records passed to SetKeys).","commonSituations":"Custom XAML reader implementations producing malformed key records; corrupted compiled BAML resources; rarely hit by ordinary application code.","solutions":["Regenerate/recompile the BAML resource (rebuild the assembly)","Ensure any custom XAML reader sets either KeyString or a valid key type on every KeyRecord","Verify the assembly's .baml resources are not corrupted or tampered with"],"exampleFix":null,"handlingStrategy":"validation","validationCode":"foreach (var k in keyRecords) { if (k?.KeyString == null && k?.KeyType == null) throw new ArgumentException(\"Invalid key record\"); }","typeGuard":null,"tryCatchPattern":"try { dict.DeferrableContent = content; } catch (ArgumentException ex) { /* log malformed BAML, rebuild resources */ }","preventionTips":["Rebuild assemblies if BAML corruption is suspected","In custom XAML readers, always set KeyString or a valid key type on KeyRecord"],"tags":["wpf","resourcedictionary","xaml","baml"],"backgroundTag":"internal-invariant-violation","analyzedSha":"81131a70a4c573cd62748a5c36908fc4d662daa9","analyzedAt":"2026-09-14T10:12:48.479Z","contentChangedAt":"2026-09-14T10:12:48.479Z","schemaVersion":2},"datasetVersion":"2026-09-22T01:17:13.364Z"}