{"record":{"id":"7b15ca1956825206","repo":"dotnet/wpf","slug":"sr-invalidoperation-addbackentrynocontent","errorCode":null,"errorMessage":"SR.InvalidOperation_AddBackEntryNoContent","messagePattern":"SR\\.InvalidOperation_AddBackEntryNoContent","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Navigation/NavigationService.cs","lineNumber":1411,"sourceCode":"                this.Navigate(value);\n            }\n        }\n\n        /// <summary>\n        /// Adds a new journal entry to NavigationWindow's back history.\n        /// </summary>\n        /// <param name=\"state\"> The custom content state (or view state) to be encapsulated in the\n        /// journal entry. If null, IProvideCustomContentState.GetContentState() will be called on\n        /// the NavigationWindow.Content or Frame.Content object.\n        /// </param>\n        public void AddBackEntry(CustomContentState state)\n        {\n            if (IsDisposed)\n            {\n                return;\n            }\n            if (_bp == null)\n                throw new InvalidOperationException(SR.InvalidOperation_AddBackEntryNoContent);\n\n            _customContentStateToSave = state;\n            JournalEntry je = UpdateJournal(NavigationMode.New, JournalReason.AddBackEntry, null);\n            // Controls state is not saved by design (saveContent=false). If client applications\n            // require it to be synchronized with the CustomContentState, they can explicitly\n            // include it.\n\n            _customContentStateToSave = null;\n\n            // Since state=null is allowed on input, make sure we get an object either via the\n            // IProvideCustomContentState interface or from a Navigating event handler.\n            // Otherwise it doesn't make sense to add a journal entry.\n            if (je != null && je.CustomContentState == null)\n            {\n                RemoveBackEntry();\n                throw new InvalidOperationException(\n                    SR.Format(SR.InvalidOperation_MustImplementIPCCSOrHandleNavigating,\n                            _bp != null ? _bp.GetType().ToString() : \"null\"));","sourceCodeStart":1393,"sourceCodeEnd":1429,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Navigation/NavigationService.cs#L1393-L1429","documentation":"NavigationService.AddBackEntry() saves a CustomContentState onto the back stack, but requires the service to currently have content (_bp != null). If nothing has been navigated to yet, it throws InvalidOperationException AddBackEntryNoContent because there is no content state to journal.","triggerScenarios":"Calling NavigationService.AddBackEntry(state) before any successful navigation set the content (_bp == null), e.g. on a fresh Frame/NavigationWindow with no page loaded.","commonSituations":"Calling AddBackEntry in a constructor or startup code before the first Navigate completes; using a frame whose content was set directly (frame.Content = x) bypassing journal creation; calling after journal disposal.","solutions":["Call AddBackEntry only after a navigation has completed (LoadCompleted) and content exists","Navigate to an initial page before attempting to add back entries","Ensure content was loaded via NavigationService.Navigate, not by setting Frame.Content directly"],"exampleFix":"// before\nvar svc = frame.NavigationService;\nsvc.AddBackEntry(new MyState()); // no content yet\n// after\nframe.Navigate(new Page1());\nframe.NavigationService.LoadCompleted += (s, e) =>\n    frame.NavigationService.AddBackEntry(new MyState());","handlingStrategy":"validation","validationCode":"bool hasContent = navService.Content != null && navService.Source != null;\nif (hasContent) navService.AddBackEntry(state);","typeGuard":"bool CanAddBackEntry(NavigationService ns) => ns.Content != null;","tryCatchPattern":"try { ns.AddBackEntry(state); }\ncatch (InvalidOperationException) { /* no content yet; navigate first */ }","preventionTips":["Call AddBackEntry only after LoadCompleted","Ensure initial content was loaded through Navigate, not Frame.Content","Guard journal APIs in startup paths"],"tags":["wpf","navigation","journal","content-state"],"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"}