Skip to content

Ability to check whether a specific navigation route has been registered #20984

@Pantheas

Description

@Pantheas

Description

It would be cool to have the ability to check whether a certain route has already been registered.
In my scenario I am doing route registration automatically via a navigation service and extended ContentPage.
However, due to the current API and behavior of .NET MAUI, there is a scenario in which the code attempts to register the same route twice.
I don't really want to catch exceptions and check for string equality since it would also swallow developer's mistakes.
I'd rather have an API to check for a specific route whether it has already been registered or not before calling Routing.RegisterRoute.
Alternatively Routing.RegisterRoute could have a parameter to set whether it should throw on duplicate registration attempt or not.

Public API Changes

public static void IsRouteRegistered(string route, RouteFactory factory, bool throwOnDuplicateRegistration = true)
public static void IsRouteRegistered(string route, Type type, bool throwOnDuplicateRegistration = true)

or

public static void RegisterRoute(string route,

Intended Use-Case

I am using Shell with Flyout and want a specific page to be created on app startup.
However, I have an extended ContentPage where I am passing a ViewModel instance as constructor parameter to be able to automatically resolve it using the IServiceProvider during Shell navigation.
To still be able to pre-create the page during app startup, I am now setting the BindingContext via XAML and in OnBindingContextChanged registering the route to the page.
The OnBindingContextChanged, however, does get called more than once and therefor can throw an exception on attempting to register the route.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions