'Xamarin Prism get the list of register pages

Is there a way I can have the list of registered pages for navigation in Prism? Or a function where it returns true when it receives a name of a page that is already registered for navigation?

This is the scenario: I'm going to perform deep linking. The app receives URL that contains the page/pages name and data like ID. Now my goal is to check if the parameters in the URL is a page or a data like an ID?

https://mycoolApp.page.link/VendorPage/1245/ProductPage/ck234

Vendor and Product are registered for navigation. 1245 and ck234 are not.

Thank you in advance.



Solution 1:[1]

Maybe you can check path ?

            var currentPage = NavigationService.GetNavigationUriPath();
            if (currentPage.Contains("ProductPage"))
            {
                // Do something
            }

Solution 2:[2]

Is there a way I can have the list of registered pages for navigation in Prism? Or a function where it returns true when it receives a name of a page that is already registered for navigation?

You can abuse the ViewModelLocationProvider like this:

((Dictionary<string, Type>)typeof(Prism.Mvvm.ViewModelLocationProvider).GetField( "_typeFactories", BindingFlags.Static | BindingFlags.NonPublic ).GetValue( null )).ContainsKey( name ) // true if a view has been registered using 'name'

(Given you register your views with view models explicitly)

You can roll out your own registry, too:

public static void MyRegisterPage<ViewType, ViewModelType>( this IContainerRegistry container, string name )
{
    _registry.Add( name );
    container.RegisterForNavigation<ViewType, ViewModelType>( name );
}

public static bool IsRegistered( string name ) => _registry.Contains( name );

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 LeRoy
Solution 2 Haukinger