dotnet/wpf · error · ArgumentException

SR.InvalidLocatorPart (Count)

Error message

SR.InvalidLocatorPart (Count)

What it means

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').

Solutions

  1. Build locator parts via FixedTextSelectionProcessor.GenerateLocatorParts rather than by hand.
  2. Before resolving, set locatorPart.NameValuePairs["Count"] to the number of text segments.
  3. Validate the stored annotation XML preserves all NameValuePairs.

Example fix

// before
var part = new ContentLocatorPart(FixedTextSelectionProcessor.FixedTextElementName);
part.NameValuePairs["Segment0"] = "...";
// after
var part = new ContentLocatorPart(FixedTextSelectionProcessor.FixedTextElementName);
part.NameValuePairs["Count"] = "1";
part.NameValuePairs["Segment0"] = "...";
Defensive patterns

Strategy: validation

Validate before calling

if (part.NameValuePairs["Count"] is null) throw new InvalidOperationException("LocatorPart missing Count attribute");

Try / catch

try { processor.ResolveLocatorPart(part, node, out var level); } catch (ArgumentException ex) { /* rebuild locator part via GenerateLocatorParts */ }

Prevention

When it happens

Trigger: Calling ResolveLocatorPart with a FixedText ContentLocatorPart whose NameValuePairs dictionary has no 'Count' key (locator part created manually or truncated).

Common situations: Hand-constructing ContentLocatorParts instead of using GenerateLocatorParts; deserializing annotation XML whose locator part lost attributes; version changes in stored annotations.

Understand the failure class

Background: "missing required argument" and "the following required arguments were not provided": what required-argument errors mean and how to fix them — this error's family across 20 libraries.

Related errors


AI-assisted analysis of dotnet/wpf@81131a70a4 (2026-09-14). Data as JSON: /api/errors/83e66ae737336863. Report an issue: GitHub.

Appendix: source

Thrown at src/Microsoft.DotNet.Wpf/src/PresentationFramework/MS/Internal/Annotations/Anchoring/FixedTextSelectionProcessor.cs:303

            attachmentLevel = AttachmentLevel.Unresolved;

            ITextView tv = (ITextView)((IServiceProvider)docPage).GetService(typeof(ITextView));
            Debug.Assert(tv != null);

            ReadOnlyCollection<TextSegment> ts = tv.TextSegments;

            //check first if a TextRange can be generated
            if (ts == null || ts.Count <= 0)
                return null;

            TextAnchor resolvedAnchor = new TextAnchor();

            if (docPage != null)
            {
                string stringCount = locatorPart.NameValuePairs["Count"];
                if (stringCount == null)
                    throw new ArgumentException(SR.Format(SR.InvalidLocatorPart, TextSelectionProcessor.CountAttribute));
                int count = Int32.Parse(stringCount, NumberFormatInfo.InvariantInfo);

                for (int i = 0; i < count; i++)
                {
                    // First we extract the start and end Point from the locator part.
                    Point start;
                    Point end;
                    GetLocatorPartSegmentValues(locatorPart, i, out start, out end);

                    //calulate start ITextPointer
                    ITextPointer segStart;
                    if (double.IsNaN(start.X) || double.IsNaN(start.Y))
                    {
                        //get start of the page
                        segStart = FindStartVisibleTextPointer(docPage);
                    }
                    else
                    {

View on GitHub (pinned to 81131a70a4)