{"record":{"id":"64c9b51d8885422b","repo":"dotnet/wpf","slug":"sr-onlyflowfixedsupported","errorCode":null,"errorMessage":"SR.OnlyFlowFixedSupported","messagePattern":"SR\\.OnlyFlowFixedSupported","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Annotations/AnnotationHelper.cs","lineNumber":1098,"sourceCode":"            ArgumentNullException.ThrowIfNull(service);\n\n            if (!service.IsEnabled)\n            {\n                throw new ArgumentException(SR.AnnotationServiceNotEnabled, nameof(service));\n            }\n\n            DocumentViewerBase viewer = service.Root as DocumentViewerBase;\n            if (viewer == null)\n            {\n                FlowDocumentScrollViewer scrollViewer = service.Root as FlowDocumentScrollViewer;\n                FlowDocumentReader reader = service.Root as FlowDocumentReader;\n                Invariant.Assert((scrollViewer != null) || (reader != null), \"Service's Root must be either a FlowDocumentReader, DocumentViewerBase or a FlowDocumentScrollViewer.\");\n            }\n            else\n            {\n                if (viewer.Document == null)\n                {\n                    throw new InvalidOperationException(SR.OnlyFlowFixedSupported);\n                }\n            }\n        }\n\n        /// <summary>\n        ///     Determines if a command should be enabled based on four things:\n        ///      1.  Existing of a service\n        ///      2.  Service is enabled\n        ///      3.  Selection available\n        ///      4.  Selection is not empty (optional)\n        /// </summary>\n        /// <param name=\"sender\">DocumentViewerBase the command will operate on</param>\n        /// <param name=\"checkForEmpty\">whether to check for empty selection or not</param>\n        /// <returns>true if the command should be enabled; false otherwise</returns>\n        private static bool IsCommandEnabled(object sender, bool checkForEmpty)\n        {\n            Invariant.Assert(sender != null, \"Parameter 'sender' is null.\");\n","sourceCodeStart":1080,"sourceCodeEnd":1116,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Annotations/AnnotationHelper.cs#L1080-L1116","documentation":"AnnotationHelper.CheckInputs throws InvalidOperationException when the enabled AnnotationService's Root viewer has a null Document. Annotation helpers only work against flow or fixed documents hosted in DocumentViewerBase, FlowDocumentReader, or FlowDocumentScrollViewer; if the viewer currently hosts no document there is nothing to annotate. The Invariant.Assert above guarantees the root is a supported viewer type, so a null Document is reported via this dedicated exception.","triggerScenarios":"Calling an AnnotationHelper method while the service is enabled but its Root viewer (e.g. DocumentViewerBase.Document or FlowDocumentScrollViewer.Document) is null — the viewer has no document loaded.","commonSituations":"Invoking annotation commands from UI wiring before the document is set on the viewer; document unloaded asynchronously while a helper is invoked; binding or navigation that clears Document during startup.","solutions":["Ensure the viewer's Document property is set (and non-null) before calling AnnotationHelper methods.","Delay annotation commands until document load completes (e.g. handle Loaded event of the viewer).","Guard helper calls with a null check on the viewer's Document.","Verify the annotation service is attached to the viewer that actually hosts the document, not a different instance."],"exampleFix":"// before\nAnnotationHelper.CreateTextStickyNoteForSelection(service, author, anchorRes, rects);\n\n// after\nif (fdvViewer.Document != null)\n    AnnotationHelper.CreateTextStickyNoteForSelection(service, author, anchorRes, rects);","handlingStrategy":"validation","validationCode":"var viewer = service.Root as DocumentViewerBase ?? (DependencyObject)service.Root as FlowDocumentScrollViewer;\nbool documentLoaded = viewer != null &&\n    (viewer is DocumentViewerBase dv ? dv.Document != null : ((FlowDocumentScrollViewer)viewer).Document != null);\nif (!documentLoaded) return; // wait for document load","typeGuard":"bool HasDocument(AnnotationService s) =>\n    (s.Root as DocumentViewerBase)?.Document != null ||\n    (s.Root as FlowDocumentScrollViewer)?.Document != null;","tryCatchPattern":"try { AnnotationHelper.CreateTextStickyNoteForSelection(service, ...); }\ncatch (InvalidOperationException) { /* defer until document is loaded */ }","preventionTips":["Hook annotation commands to fire only after the viewer's Document is assigned.","Set the Document before enabling the annotation service.","Re-check Document after async navigation or load operations."],"tags":["wpf","annotations","document-viewer","invalid-state"],"backgroundTag":"incompatible-source-type","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"}