使用 Prism 获取用于导航的注册视图列表

Get the list of registered view for navigation with Prism

我正在 WPF 上使用 Prism DryIoc 开发模块化应用程序

在我的模块中,我注册了这样的导航视图

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

有没有办法直接从 Prism 或 Container 找到当前注册的导航视图列表?

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

不使用 IContainerRegistry 接口但使用 DryIoc 实现:

if (containerRegistry is DryIocContainerExtension dryIoc)
{
    IContainer container = dryIoc.Instance;
    Type[] types = container.GetServiceRegistrations()?
        .Where(x => !string.IsNullOrEmpty(x.OptionalServiceKey?.ToString()))
        .Select(x => x.ImplementationType)
        .ToArray();
}

您应该为您希望用户能够从中 select 访问的那些视图推出自己的注册表。那个也可以注册导航,不用重复注册码

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
}

并在应用程序或引导程序的 RegisterTypes

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

并在模块中'

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

注意:RegisterTypes中的Resolve是邪恶且容易出错的,但这里不能避免。

注意:你不能注入IContainerRegistry,因此我们使用RegisterInstance(注册容器注册表会非常邪恶)