Mystline
Mystline

Reputation: 13

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

In prism 4.1 I want to use module loading and build my project using mvvm. Now I register the module using directory scanning,but navigation is not possible in the view model

the code for the module

[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");
    }
}

Then I start in 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" };
    }
}

I wrote it like this in the view model

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 in MainWindow:

<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);
    }

I can't use the buttons to navigate right now, nothing happens after clicking. Is it because I made a mistake when registering the module? Or is the module not found for other reasons?

Upvotes: 1

Views: 205

Answers (1)

Haukinger
Haukinger

Reputation: 10863

You want to register ViewA for navigation, not add it to the region immediately.

That is:

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

then, upon RequestNavigate, the container will create a new instance and the region manager will put it in the region.

Upvotes: 0

Related Questions