1
votes

I am new to Prism and I am trying to determine a best practice for deactivating a view in a Prism 2 application -- when a user clicks a button on the view I want to deactivate the view. The view is executing a command when the button is clicked.

The view model is receiving the command but the viewmodel does not have a reference to the regionmanager.

Should the view model know about the region manager? And even if the viewmodel had a reference to it, it needs the view instance to pass to the deactive method on the containing region.

I am using the MVVM pattern for the app.

3

3 Answers

4
votes

I'm gonna go on a limb here and assume that you are using an itemscontrol or such for your region, because that's the most obvious use of a "button that removes a view".

In that case, you should have a Controller (or whatever you want to call it, but the Prism team seems to use that kind of name for that kind of responsibility) somewhere in charge of managing the active ViewModels, and either you display your views using DataTemplates, or you manually create/add/activate a new view when adding a new instance. Your case seems to be the second one, and the controller should be responsible for managing the views. Your viewmodel should get a reference to that controller through Dependency Injection and ask it to remove it from the pool of active models/views.

The Controller itself receives the IRegionManager and finds the Region it is responsible for.

I hope that makes sense, please comment if it does not.

2
votes

EventAggregator in your case (control which needs to unload itself) is maybe an overkill because simple injecting of IRegionManager to that control view model constructor

ctor (IRegionManager regionManager)
{
    this.RegionManager = regionManager
}

and then something like this

this.regionManager.Regions[regionName].Remove(this.View);

should do the thing you asked for.

IRegionManager is mockable interface enabling easy testing and it is just an abstraction not coupling you to implementation and enabling IoC.

0
votes

I got hung up here too. I found that because the RegionManager was responsible for the Regions it contained I was ending up with tight coupling between my RegionManager and the Regions I wanted to manage.

It was a complex job to manage these regions externally (from say a main application menu) and as a result we dropped the PRISM framework and wrote our own code based on the Composite Application Guidance elements that were of use in our particular scenario.