{"record":{"id":"83e66ae737336863","repo":"dotnet/wpf","slug":"sr-invalidlocatorpart-count","errorCode":null,"errorMessage":"SR.InvalidLocatorPart (Count)","messagePattern":"SR\\.InvalidLocatorPart \\(Count\\)","errorType":"exception","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/PresentationFramework/MS/Internal/Annotations/Anchoring/FixedTextSelectionProcessor.cs","lineNumber":303,"sourceCode":"\n            attachmentLevel = AttachmentLevel.Unresolved;\n\n            ITextView tv = (ITextView)((IServiceProvider)docPage).GetService(typeof(ITextView));\n            Debug.Assert(tv != null);\n\n            ReadOnlyCollection<TextSegment> ts = tv.TextSegments;\n\n            //check first if a TextRange can be generated\n            if (ts == null || ts.Count <= 0)\n                return null;\n\n            TextAnchor resolvedAnchor = new TextAnchor();\n\n            if (docPage != null)\n            {\n                string stringCount = locatorPart.NameValuePairs[\"Count\"];\n                if (stringCount == null)\n                    throw new ArgumentException(SR.Format(SR.InvalidLocatorPart, TextSelectionProcessor.CountAttribute));\n                int count = Int32.Parse(stringCount, NumberFormatInfo.InvariantInfo);\n\n                for (int i = 0; i < count; i++)\n                {\n                    // First we extract the start and end Point from the locator part.\n                    Point start;\n                    Point end;\n                    GetLocatorPartSegmentValues(locatorPart, i, out start, out end);\n\n                    //calulate start ITextPointer\n                    ITextPointer segStart;\n                    if (double.IsNaN(start.X) || double.IsNaN(start.Y))\n                    {\n                        //get start of the page\n                        segStart = FindStartVisibleTextPointer(docPage);\n                    }\n                    else\n                    {","sourceCodeStart":285,"sourceCodeEnd":321,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationFramework/MS/Internal/Annotations/Anchoring/FixedTextSelectionProcessor.cs#L285-L321","documentation":"When resolving a FixedText locator part, ResolveLocatorPart reads the 'Count' attribute from locatorPart.NameValuePairs. If the attribute is missing it cannot know how many segments to parse and throws ArgumentException(SR.InvalidLocatorPart, 'Count').","triggerScenarios":"Calling ResolveLocatorPart with a FixedText ContentLocatorPart whose NameValuePairs dictionary has no 'Count' key (locator part created manually or truncated).","commonSituations":"Hand-constructing ContentLocatorParts instead of using GenerateLocatorParts; deserializing annotation XML whose locator part lost attributes; version changes in stored annotations.","solutions":["Build locator parts via FixedTextSelectionProcessor.GenerateLocatorParts rather than by hand.","Before resolving, set locatorPart.NameValuePairs[\"Count\"] to the number of text segments.","Validate the stored annotation XML preserves all NameValuePairs."],"exampleFix":"// before\nvar part = new ContentLocatorPart(FixedTextSelectionProcessor.FixedTextElementName);\npart.NameValuePairs[\"Segment0\"] = \"...\";\n// after\nvar part = new ContentLocatorPart(FixedTextSelectionProcessor.FixedTextElementName);\npart.NameValuePairs[\"Count\"] = \"1\";\npart.NameValuePairs[\"Segment0\"] = \"...\";","handlingStrategy":"validation","validationCode":"if (part.NameValuePairs[\"Count\"] is null) throw new InvalidOperationException(\"LocatorPart missing Count attribute\");","typeGuard":null,"tryCatchPattern":"try { processor.ResolveLocatorPart(part, node, out var level); } catch (ArgumentException ex) { /* rebuild locator part via GenerateLocatorParts */ }","preventionTips":["Always construct locator parts with GenerateLocatorParts","Round-trip NameValuePairs intact through serialization","Prefer generating fresh anchors over hand-editing stored locator parts"],"tags":["wpf","annotations","locator-part"],"backgroundTag":"missing-required-argument","analyzedSha":"81131a70a4c573cd62748a5c36908fc4d662daa9","analyzedAt":"2026-09-14T10:12:48.479Z","contentChangedAt":"2026-09-14T10:12:48.479Z","schemaVersion":2},"datasetVersion":"2026-09-21T21:30:21.729Z"}