views:

498

answers:

1

I need to instantiate a new view on a command

Imagine I have a "new employee" button in a module and when you press it, I want to create a new employee view, you press it 3 times and I want to have a tab with three elements in it and each tab page contains data for an employee, then you can save and/or close each tab page separately.

how do I do this with Prism?

+2  A: 

The ViewInjectionComposition QuickStart has a great example of what you are looking for.

What you do is delegate a command to a controller, get your scoped region out of the region manager. Once you have the scoped region, resolve a new view and add it to the region.

Here is a snippet from the quickstart that you could easily modify to do what you are looking for.

 public class EmployeesController : IEmployeesController
{
    private IUnityContainer container;
    private IRegionManager regionManager;

    public EmployeesController(IUnityContainer container, IRegionManager regionManager)
    {
        this.container = container;
        this.regionManager = regionManager;
    }

    public virtual void OnEmployeeSelected(BusinessEntities.Employee employee)
    {
        IRegion detailsRegion = regionManager.Regions[RegionNames.DetailsRegion];
        object existingView = detailsRegion.GetView(employee.EmployeeId.ToString(CultureInfo.InvariantCulture));

        if (existingView == null)
        {
            IProjectsListPresenter projectsListPresenter = this.container.Resolve<IProjectsListPresenter>();
            projectsListPresenter.SetProjects(employee.EmployeeId);

            IEmployeesDetailsPresenter detailsPresenter = this.container.Resolve<IEmployeesDetailsPresenter>();
            detailsPresenter.SetSelectedEmployee(employee);

            IRegionManager detailsRegionManager = detailsRegion.Add(detailsPresenter.View, employee.EmployeeId.ToString(CultureInfo.InvariantCulture), true);
            IRegion region = detailsRegionManager.Regions[RegionNames.TabRegion];
            region.Add(projectsListPresenter.View, "CurrentProjectsView");
            detailsRegion.Activate(detailsPresenter.View);
        }
        else
        {
            detailsRegion.Activate(existingView);
        }
    }
}
Ryan Rauh