dotnet/wpf · error · InvalidOperationException

SR.InvalidOperation_AddBackEntryNoContent

Error message

SR.InvalidOperation_AddBackEntryNoContent

What it means

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.

Solutions

  1. Call AddBackEntry only after a navigation has completed (LoadCompleted) and content exists
  2. Navigate to an initial page before attempting to add back entries
  3. Ensure content was loaded via NavigationService.Navigate, not by setting Frame.Content directly

Example fix

// before
var svc = frame.NavigationService;
svc.AddBackEntry(new MyState()); // no content yet
// after
frame.Navigate(new Page1());
frame.NavigationService.LoadCompleted += (s, e) =>
    frame.NavigationService.AddBackEntry(new MyState());
Defensive patterns

Strategy: validation

Validate before calling

bool hasContent = navService.Content != null && navService.Source != null;
if (hasContent) navService.AddBackEntry(state);

Type guard

bool CanAddBackEntry(NavigationService ns) => ns.Content != null;

Try / catch

try { ns.AddBackEntry(state); }
catch (InvalidOperationException) { /* no content yet; navigate first */ }

Prevention

When it happens

Trigger: Calling NavigationService.AddBackEntry(state) before any successful navigation set the content (_bp == null), e.g. on a fresh Frame/NavigationWindow with no page loaded.

Common situations: 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.

Understand the failure class

Background: "Invalid state transition" errors: "status must be X, actually Y", "already rejected/charging/uninstalled", "cannot ... while running" — what they mean when a library rejects your call — this error's family across 31 libraries.

Related errors


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

Appendix: source

Thrown at src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Navigation/NavigationService.cs:1411

                this.Navigate(value);
            }
        }

        /// <summary>
        /// Adds a new journal entry to NavigationWindow's back history.
        /// </summary>
        /// <param name="state"> The custom content state (or view state) to be encapsulated in the
        /// journal entry. If null, IProvideCustomContentState.GetContentState() will be called on
        /// the NavigationWindow.Content or Frame.Content object.
        /// </param>
        public void AddBackEntry(CustomContentState state)
        {
            if (IsDisposed)
            {
                return;
            }
            if (_bp == null)
                throw new InvalidOperationException(SR.InvalidOperation_AddBackEntryNoContent);

            _customContentStateToSave = state;
            JournalEntry je = UpdateJournal(NavigationMode.New, JournalReason.AddBackEntry, null);
            // Controls state is not saved by design (saveContent=false). If client applications
            // require it to be synchronized with the CustomContentState, they can explicitly
            // include it.

            _customContentStateToSave = null;

            // Since state=null is allowed on input, make sure we get an object either via the
            // IProvideCustomContentState interface or from a Navigating event handler.
            // Otherwise it doesn't make sense to add a journal entry.
            if (je != null && je.CustomContentState == null)
            {
                RemoveBackEntry();
                throw new InvalidOperationException(
                    SR.Format(SR.InvalidOperation_MustImplementIPCCSOrHandleNavigating,
                            _bp != null ? _bp.GetType().ToString() : "null"));

View on GitHub (pinned to 81131a70a4)