{"record":{"id":"0e7158ac513138df","repo":"dotnet/wpf","slug":"sr-methodcallnotallowed","errorCode":null,"errorMessage":"SR.MethodCallNotAllowed","messagePattern":"SR\\.MethodCallNotAllowed","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/Diagnostics/VisualDiagnostics.cs","lineNumber":254,"sourceCode":"        // this class does all the work for checking whether VisualTreeChanged features are\n        // enabled, disallowed, etc.  It's a separate class so that its static\n        // cctor doesn't run until needed, giving apps time to initialize factors\n        // that influence the decisions:  environment, registry, app-context switches, etc.\n        private static class EnableHelper\n        {\n            static EnableHelper()\n            {\n                if (IsEnabled)\n                {\n                    s_IsDevMode = GetDevModeFromRegistry();\n                    s_IsEnableVisualTreeChangedAllowed = PrecomputeIsEnableVisualTreeChangedAllowed();\n                }\n            }\n\n            internal static void EnableVisualTreeChanged()\n            {\n                if (!IsEnableVisualTreeChangedAllowed)\n                    throw new InvalidOperationException(SR.Format(SR.MethodCallNotAllowed, nameof(VisualDiagnostics.EnableVisualTreeChanged)));\n\n                s_IsVisualTreeChangedEnabled = true;\n            }\n\n            internal static void DisableVisualTreeChanged()\n            {\n                s_IsVisualTreeChangedEnabled = false;\n            }\n\n            internal static bool IsVisualTreeChangeEnabled\n            {\n                get\n                {\n                    return IsEnabled &&\n                       (s_IsVisualTreeChangedEnabled ||\n                        System.Diagnostics.Debugger.IsAttached ||\n                        s_isDebuggerCheckDisabledForTestPurposes);\n                }","sourceCodeStart":236,"sourceCodeEnd":272,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/Diagnostics/VisualDiagnostics.cs#L236-L272","documentation":"VisualDiagnostics.EnableVisualTreeChanged throws InvalidOperationException when called outside an active VisualTreeChanged callback (IsEnableVisualTreeChangedAllowed is false). Visual tree change notification can only be enabled from within a Changed handler so it cannot be left permanently on by arbitrary code.","triggerScenarios":"Calling VisualDiagnostics.EnableVisualTreeChanged() from application code at startup or in an arbitrary method, rather than from inside a VisualTreeChanged event handler callback.","commonSituations":"Copy-pasted diagnostic setup code enabling changed-notification eagerly; tests calling the enable method directly; tooling that assumed it is a public toggle.","solutions":["Call EnableVisualTreeChanged only from inside a VisualTreeChanged handler callback","Wrap in try-catch InvalidOperationException if you only opportunistically enable it","Do not rely on EnableVisualTreeChanged for standalone diagnostics; subscribe to VisualTreeChanged events instead"],"exampleFix":"// before\nVisualDiagnostics.EnableVisualTreeChanged(); // at app startup\n// after\n// call this only from within the VisualTreeChanged callback:\nVisualDiagnostics.EnableVisualTreeChanged();","handlingStrategy":"try-catch","validationCode":"if (!insideVisualTreeChangedCallback)\n{\n    throw new InvalidOperationException(\"EnableVisualTreeChanged may only be called inside a VisualTreeChanged handler.\");\n}","typeGuard":null,"tryCatchPattern":"try { VisualDiagnostics.EnableVisualTreeChanged(); }\ncatch (InvalidOperationException) { /* not inside a Changed callback; skip */ }","preventionTips":["Only call EnableVisualTreeChanged from within a VisualTreeChanged callback","Do not attempt to enable changed-notification from app startup code","Use VisualDiagnostics.IsEnabled and event subscription for general diagnostics"],"tags":["wpf","invalidoperationexception","visual-tree","diagnostics"],"backgroundTag":"invalid-state-transition","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"}