{"record":{"id":"3c4ece79a6be64f2","repo":"dotnet/wpf","slug":"sr-textviewinvalidlayout-textviewbase","errorCode":null,"errorMessage":"SR.TextViewInvalidLayout","messagePattern":"SR\\.TextViewInvalidLayout","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/PresentationFramework/MS/Internal/documents/TextViewBase.cs","lineNumber":109,"sourceCode":"        /// <summary>\n        /// <see cref=\"ITextView.GetBackspaceCaretUnitPosition\"/>\n        /// </summary>\n        internal abstract ITextPointer GetBackspaceCaretUnitPosition(ITextPointer position);\n\n        /// <summary>\n        /// <see cref=\"ITextView.GetLineRange\"/>\n        /// </summary>\n        internal abstract TextSegment GetLineRange(ITextPointer position);\n\n        /// <summary>\n        /// <see cref=\"ITextView.GetGlyphRuns\"/>\n        /// </summary>\n        internal virtual ReadOnlyCollection<GlyphRun> GetGlyphRuns(ITextPointer start, ITextPointer end)\n        {\n            // Verify that layout information is valid. Cannot continue if not valid.\n            if (!IsValid)\n            {\n                throw new InvalidOperationException(SR.TextViewInvalidLayout);\n            }\n            return ReadOnlyCollection<GlyphRun>.Empty;\n        }\n\n        /// <summary>\n        /// <see cref=\"ITextView.Contains\"/>\n        /// </summary>\n        internal abstract bool Contains(ITextPointer position);\n\n        /// <summary>\n        /// Scroll the given rectangle the minimum amount required to bring it entirely into view.\n        /// </summary>\n        /// <param name=\"textView\">TextView doing the scrolling</param>\n        /// <param name=\"rect\">Rect to scroll</param>\n        /// <remarks>\n        /// # RECT POSITION       RECT SIZE        SCROLL      REMEDY\n        /// 1 Above viewport      lte viewport     Down        Align top edge of rect and viewport\n        /// 2 Above viewport      gt viewport      Down        Align bottom edge of rect and viewport","sourceCodeStart":91,"sourceCodeEnd":127,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationFramework/MS/Internal/documents/TextViewBase.cs#L91-L127","documentation":"TextViewBase.GetGlyphRuns throws InvalidOperationException(SR.TextViewInvalidLayout) when IsValid is false. Glyph run enumeration is only meaningful against current rendered geometry; the base view refuses to return stale (or empty) results when layout has been invalidated. Subclasses such as TextParagraphView inherit the same guard. Refresh layout before enumerating glyph runs.","triggerScenarios":"Calling GetGlyphRuns (used by rendering/spell-check underline drawing) after layout invalidation — text edit, font/theme change, resize — or before the first layout pass completed.","commonSituations":"Custom adorners drawing squiggles while the editor is re-flowing; screen-reader or automation code enumerating glyphs on a not-yet-rendered control; rendering hooks firing during layout transitions.","solutions":["Check textView.IsValid and call Validate() prior to GetGlyphRuns.","Call UpdateLayout() on the host control so the view rebuilds its layout.","Hook TextView.Updated and enumerate glyph runs only while IsValid is true.","Catch InvalidOperationException, await re-layout (Dispatcher at Loaded priority), then retry."],"exampleFix":"// before\nvar runs = textView.GetGlyphRuns(start, end);\n// after\nif (!textView.IsValid) textView.Validate();\nvar runs = textView.IsValid ? textView.GetGlyphRuns(start, end) : null;","handlingStrategy":"validation","validationCode":"if (!textView.IsValid) textView.Validate();\nif (!textView.IsValid) return Array.Empty<GlyphRun>();","typeGuard":"static bool ReadyForGlyphEnumeration(ITextView view) => view != null && view.IsValid;","tryCatchPattern":"try { runs = textView.GetGlyphRuns(start, end); }\ncatch (InvalidOperationException) { runs = null; /* wait for Updated */ }","preventionTips":["Enumerate glyph runs in render/Updated hooks, not in edit handlers","Validate layout before custom adorner drawing","Avoid caching GlyphRun results across layout invalidations","Check IsValid after font/theme changes"],"tags":["wpf","textview","layout","rendering"],"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-21T21:30:21.729Z"}