dotnet/wpf · error · InvalidOperationException

SR.AddAnnotationsNotImplemented

Error message

SR.AddAnnotationsNotImplemented

What it means

StickyNoteControl.AddAttachedAnnotation only supports adding annotations whose anchor component is this StickyNote (annotation loaded from the AnnotationService store); it throws InvalidOperationException('AddAnnotationsNotImplemented') for any other input path. The method exists to implement the IAnnotationComponent interface, not as a general API for programmatically creating annotations.

Solutions

  1. Do not call AddAttachedAnnotation directly; use AnnotationService (CreateHighlightForSelection/CreateTextStickyNoteForSelection) to create annotations.
  2. Let AnnotationService/AnnotationComponentManager load annotations from the annotation store, which is the only supported path.
  3. If you need custom anchors, implement a custom IAnnotationComponent and register it via AnnotationComponentManager rather than reusing StickyNoteControl's.
  4. Check that AnnotationService is enabled on the FlowDocumentPageViewer/DocumentViewer hosting the StickyNote before expecting annotation loading.
Defensive patterns

Strategy: try-catch

Validate before calling

// Only let AnnotationService load annotations; if calling directly, verify the annotation was loaded by AnnotationComponentManager first

Try / catch

try { stickyNote.AddAttachedAnnotation(attachedAnnotation); }
catch (InvalidOperationException) { annotationService.CreateTextStickyNoteForSelection(anchorId, ...); }

Prevention

When it happens

Trigger: Calling StickyNoteControl.AddAttachedAnnotation (explicitly or via the IAnnotationComponent interface) with an attached annotation that is not being loaded by the AnnotationService/AnnotationComponentManager for this StickyNote.

Common situations: Application code trying to create annotations on a StickyNote programmatically instead of using AnnotationService.CreateHighlightForSelection, or calling the interface method directly after loading annotations from a stream.

Related errors


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

Appendix: source

Thrown at src/Microsoft.DotNet.Wpf/src/PresentationFramework/MS/Internal/Controls/StickyNote/StickyNoteAnnotations.cs:919

        /// </summary>
        /// <param name="attachedAnnotation">An IAttachedAnnotation instance</param>
        void IAnnotationComponent.AddAttachedAnnotation(IAttachedAnnotation attachedAnnotation)
        {
            ArgumentNullException.ThrowIfNull(attachedAnnotation);

            if (_attachedAnnotation == null)
            {
                //fire trace event
                EventTrace.EasyTraceEvent(EventTrace.Keyword.KeywordAnnotation, EventTrace.Event.AddAttachedSNBegin);

                SetAnnotation(attachedAnnotation);

                //fire trace event
                EventTrace.EasyTraceEvent(EventTrace.Keyword.KeywordAnnotation, EventTrace.Event.AddAttachedSNEnd);
            }
            else
            {
                throw new InvalidOperationException(SR.AddAnnotationsNotImplemented);
            }
        }

        /// <summary>
        /// Removes an attached annotations from this StickyNoteControl.
        /// </summary>
        /// <param name="attachedAnnotation">An IAttachedAnnotation instance</param>
        void IAnnotationComponent.RemoveAttachedAnnotation(IAttachedAnnotation attachedAnnotation)
        {
            ArgumentNullException.ThrowIfNull(attachedAnnotation);

            if (attachedAnnotation == _attachedAnnotation)
            {
                //fire trace event
                EventTrace.EasyTraceEvent(EventTrace.Keyword.KeywordAnnotation, EventTrace.Event.RemoveAttachedSNBegin);

                GiveUpFocus();

View on GitHub (pinned to 81131a70a4)