Search code examples
c#wpfprismmodularitycag

Using Presenter in Prism


I am developing an application using PRISM in C# and WPF. I am new to this and would like to implement the Presenter. Basically, I would like to register a Presenter instead of View in my Module.

At present I am doing the following in my Module Initialize:

iRegionManager.RegisterViewWithRegion("MainRegion", typeof(AboutWindow));

What I would like is I want to have a presenter, I will register presenter in my module. This presenter must be responsible to show the view in my region.

I tried reading several articles and examples but was not able to get exactly what I want.

Pseudo-code for my requirements is as follows:

public class AboutModule : IAboutModule
{
    IRegionManager iRegionManager = null;
    IUnityContainer container = null;

    public AboutModule(IRegionManager iRegionManager, IUnityContainer container)
    {
        this.iRegionManager = iRegionManager;
        this.container = container;
    }

    public void Initialize()
    {
        //Register my presenter here.
    }
}


internal class AboutModulePresenter : IAboutModulePresenter
{
    private IAboutModuleView iAboutModuleView = null;

    internal AboutModulePresenter(IAboutModuleView iAboutModuleView)
    {
        this.iAboutModuleView = iAboutModuleView;
    }
    public IAboutModuleView View
    {
        get
        {
            return this.iAboutModuleView;
        }
    }
    public void ShowView()
    {
        //Register my view with region manager and display in the region.
    }
}

Solution

  • You could do this. Essentially you would have to map IAboutModuleView to AboutModuleView with whatever IoC container you're using e.g. Unity. Then in your ShowView method you would call RegisterViewWithRegion on the RegionManager, passing in the view.

    However, how and where would you construct your presenter? Who would be responsible for calling ShowView()?

    I would also recommend taking a look at the MVVM pattern (whether you use VM-first or View-first is up to you) which is fairly similar to MVP but is better suited to WPF and Silverlight applications.