dotnet/wpf · error · InvalidOperationException

SR.RequestNavigateEventMustHaveRoutedEvent

Error message

SR.RequestNavigateEventMustHaveRoutedEvent

What it means

RequestNavigateEventArgs.InvokeEventHandler requires RoutedEvent to be set so it can correctly dispatch the typed handler. If the event args were raised without an associated RoutedEvent, dispatch cannot proceed and the method throws.

Solutions

  1. Use NavigationService.RequestNavigate(uri) or element.RequestNavigate instead of manual raising.
  2. If raising manually, set args.RoutedEvent = RequestNavigateEvent before RaiseEvent.
  3. Raise on an element that has the RequestNavigateEvent routed event defined.
  4. Construct args via the constructor overload that takes the RoutedEvent.

Example fix

// before
var args = new RequestNavigateEventArgs(uri, "target");
textBlock.RaiseEvent(args);
// after
var args = new RequestNavigateEventArgs(uri, "target") { RoutedEvent = Hyperlink.RequestNavigateEvent };
textBlock.RaiseEvent(args);
Defensive patterns

Strategy: type-guard

Validate before calling

if (args.RoutedEvent == null) args.RoutedEvent = Hyperlink.RequestNavigateEvent;
element.RaiseEvent(args);

Type guard

bool CanRaise(RequestNavigateEventArgs a) => a.RoutedEvent != null;

Try / catch

try { element.RaiseEvent(args); }
catch (InvalidOperationException ex) when (ex.Message.Contains("RoutedEvent"))
{ NavigationService.RequestNavigate(args.Uri, args.Target); }

Prevention

When it happens

Trigger: Calling RaiseEvent with a RequestNavigateEventArgs whose RoutedEvent property was never assigned, or invoking InvokeEventHandler manually on detached args instead of through RaiseEvent on an element with a registered RequestNavigateEvent.

Common situations: Manually newing RequestNavigateEventArgs and raising it without setting RoutedEvent; copying args between events; custom automation code invoking handlers directly.

Understand the failure class

Background: "missing required argument" and "the following required arguments were not provided": what required-argument errors mean and how to fix them — this error's family across 20 libraries.

Related errors


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

Appendix: source

Thrown at src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Navigation/RequestNavigateEventArgs.cs:68

        /// <summary>
        /// Target window or frame to perform navigation
        /// </summary>
        /// <ExternalAPI Inherit="true"/>
        public string Target
        {
            get{return _target;}
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="genericHandler"></param>
        /// <param name="genericTarget"></param>
        protected override void InvokeEventHandler(Delegate genericHandler, object genericTarget)
        {
            if (RoutedEvent == null)
            {
                throw new InvalidOperationException(SR.RequestNavigateEventMustHaveRoutedEvent);
            }

            RequestNavigateEventHandler handler = (RequestNavigateEventHandler)genericHandler;

            handler(genericTarget, this);
        }            
    }

    /// <summary>
    /// Delegate that handles RequestNavigate event.
    /// </summary>
    /// <ExternalAPI Inherit="true"/>
    public delegate void RequestNavigateEventHandler(object sender, RequestNavigateEventArgs e);
}

View on GitHub (pinned to 81131a70a4)