Prism 4.1 如何使用目录扫描加载模块并将它们应用于 mvvm 模式以进行导航

Prism 4.1 How to loading modules with directory scan andapply them to mvvm pattern for navigation

在 prism 4.1 中,我想使用模块加载并使用 mvvm 构建我的项目。 现在我使用目录扫描注册模块,但是在视图模型中无法导航

模块代码

[Module(ModuleName = "ModuleA", OnDemand = true)]
public class ModuleAModule : IModule
{
    private IRegionManager RegionManager { get; set; }
    private ViewA ViewA;

    public ModuleAModule(IRegionManager RegionManager, IUnityContainer container)
    {
        if (RegionManager != null)
        {
            this.RegionManager = RegionManager;
        }
        if (container != null)
        {
            this.container = container;
        }
    }

    public void Initialize()
    {
        this.container.RegisterType<object, ViewA>(nameof(ViewA));

        IRegion MathRegion = RegionManager.Regions["ContentRegions"];
        MathRegion.Add(this.ViewA, "ViewA");
    }
}

然后我在 Bootstrapper 中启动

 class Bootstrapper : UnityBootstrapper
{
    protected override DependencyObject CreateShell()
    {
        return ServiceLocator.Current.GetInstance<MainWindow>();
    }

    protected override void InitializeShell()
    {
        base.InitializeShell();
        Application.Current.MainWindow.Show();
    }

    protected override IModuleCatalog CreateModuleCatalog()
    {
        return new DirectoryModuleCatalog() { ModulePath = @".\Modules" };
    }
}

我在视图模型中是这样写的

class MainWindowViewModel : NotificationObject
{
    private readonly IRegionManager _regionManager;
    public DelegateCommand<string> NavigateCommand { get; private set; }

    public MainWindowViewModel(IRegionManager regionManager)
    {
        _regionManager = regionManager;
        NavigateCommand = new DelegateCommand<string>(Navigate);
    }

    private void Navigate(string navigatePath)
    {

        if (navigatePath != null)
            _regionManager.RequestNavigate("ContentRegion", new Uri(navigatePath, UriKind.Relative));
    }
}
主窗口中的

Xaml:

<DockPanel LastChildFill="True">
    <StackPanel Orientation="Horizontal" DockPanel.Dock="Top" Margin="5">
        <Button Command="{Binding NavigateCommand}" CommandParameter="ViewA" Margin="5">NavigateCommand A</Button>
    </StackPanel>
    <TabControl prism:RegionManager.RegionName="ContentRegions" Margin="5"/>
</DockPanel>

    public MainWindow(IRegionManager region)
    {
        InitializeComponent();
        this.DataContext = new MainWindowViewModel(region);
    }

我现在无法使用按钮进行导航,点击后没有任何反应。是不是我注册模块的时候出错了?还是其他原因没有找到模块?

您要注册ViewA导航,不是立即添加到区域。

即:

container.Register<object, ViewA>( nameof(ViewA) );

然后,在RequestNavigate时,容器将创建一个新实例,区域管理器会将其放入该区域。