Search code examples
wpfprismdryioc

Get the list of registered view for navigation with Prism


I am working on a modular application with Prism DryIoc on WPF

In my modules I have views registered for navigation like this

public void RegisterTypes(IContainerRegistry containerRegistry)
{
     containerRegistry.RegisterForNavigation<Manual1View>();
     containerRegistry.RegisterForNavigation<Manual2View>();
     containerRegistry.RegisterForNavigation<ProductionView>();       
}

Is there a way to find the list of currently registered views for navigation directly from Prism or the Container ?


Solution

  • You should roll out your own registry for those views you want the user to be able to select from. That one could also do the registration for navigation, so you don't have to duplicate the registration code.

    internal class MachineModeRegistry : IMachineModeRegistry
    {
        public MachineModeRegistry(IContainerRegistry containerRegistry)
        {
            _containerRegistry = containerRegistry;
        }
    
        #region IMachineModeRegistry
        public void RegisterView<T>()
        {
            _containerRegistry.RegisterViewForNavigation<T>(nameof(T));
            _listOfViews.Add( nameof(T) );
        }
    
        public IReadOnlyCollection<string> RegisteredViews => _listOfViews;
        #endregion
    
        #region private
        private readonly List<string> _listOfViews = new List<string>();
        private readonly IContainerRegistry _containerRegistry;
        #endregion
    }
    

    and in the app or bootstrapper's RegisterTypes

    _containerRegistry.RegisterInstance<IMachineModeRegistry>(new MachineModeRegistry(_containerRegistry);
    

    and in the modules'

     _containerRegistry.Resolve<IMachineRegistry>().RegisterView<Manual1View>();
    

    Note: Resolve in RegisterTypes is evil and error-prone, but can't be avoided here.

    Note: you can't inject IContainerRegistry, therefore we use RegisterInstance (registering the container registry instead would have been very evil)