dotnet/wpf · error · NotSupportedException

SR.ReturnEventHandlerMustBeOnParentPage

Error message

SR.ReturnEventHandlerMustBeOnParentPage

What it means

ReturnEventSaver._Attach wires a PageFunction's Return event handlers back onto the calling page. It matches each recorded listener by the target page's assembly-qualified type name; if the caller's type differs from the recorded target, the handler is not on the parent page and the library throws NotSupportedException(SR.ReturnEventHandlerMustBeOnParentPage).

Solutions

  1. Ensure the object resuming the PageFunction is the exact page type that called it (assembly-qualified name must match)
  2. Call the PageFunction from the parent page itself, not a child control or wrapper
  3. Restructure to keep the Return handler on the calling page rather than attaching external listeners

Example fix

// before
// Button_Click in some other page calls pageFunction.Start(...) and later resumes it
// after
// Start the PageFunction from the parent page method; handle Return on that same page
Defensive patterns

Strategy: validation

Validate before calling

bool IsCallerParentPage(object caller, string recordedTypeName) => string.Equals(caller.GetType().AssemblyQualifiedName, recordedTypeName, StringComparison.Ordinal);

Try / catch

try { ResumeFromPageFunction(); } catch (NotSupportedException) { /* caller is not the parent page; route through parent */ }

Prevention

When it happens

Trigger: Resuming a PageFunction whose Return handler was recorded against a different page type than the object now calling the resume/attach path — e.g. navigation journal restore from a page that isn't the original caller.

Common situations: PageFunction hosted in its own window or a different frame; calling page recreated or retyped between calls; custom navigation code invoking the Return flow from a foreign object.

Understand the failure class

Background: UnsupportedOperationException and "is not supported" errors: when a library deliberately refuses a call — this error's family across 30 libraries.

Related errors


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

Appendix: source

Thrown at src/Microsoft.DotNet.Wpf/src/PresentationFramework/MS/Internal/AppModel/ReturnEventSaver.cs:118

        internal void _Attach(Object caller, PageFunctionBase child)
        {
            ReturnEventSaverInfo[] list = null;

            list = _returnList;

            if (list != null)
            {
                Debug.Assert(caller != null, "Caller should not be null");
                for (int i = 0; i < list.Length; i++)
                {
                    //
                    // Future notes: how do we handle listeners that were not on the calling pagefunction ? 
                    // E.g. - if we had a listener to OnFinish from a Button on the calling page.  
                    //  "Return event never fired from PageFunction hosted in its own window"
                    // 
                    if (!string.Equals(_returnList[i]._targetTypeName, caller.GetType().AssemblyQualifiedName, StringComparison.Ordinal))
                    {
                        throw new NotSupportedException(SR.ReturnEventHandlerMustBeOnParentPage);
                    }

                    Delegate d;
                    try
                    {
                        d = Delegate.CreateDelegate(
                                                                Type.GetType(_returnList[i]._delegateTypeName),
                                                                caller,
                                                                _returnList[i]._delegateMethodName);
                    }
                    catch (Exception ex)
                    {
                        throw new NotSupportedException(SR.ReturnEventHandlerMustBeOnParentPage, ex);
                    }

                    child._AddEventHandler(d);
                }
            }

View on GitHub (pinned to 81131a70a4)