Unregister view with a region?

Jun 1, 2010 at 4:40 PM

I am new to Prism and am still learning. I have a question about views/regions/modules. Is it possible to remove modules and/or unregister views from a region at runtime? I don't necessarily need to unload the .NET assembly containing the modules to be removed, I simply need to deactivate them from the prism framework. 

The reason is that I have a document-based application where different modules need to be loaded for different documents. For example, Document1 may use modules A, B, and C, and Document2 may use modules B, E, and F. Which modules are required for each document is specified within the document itself. When Document1 is opened, I will need to load modules A, B, and C. When Document1 is closed and then Document2 is opened, I would like to 'unload' modules A and C and then load modules E and F. Effectively, I no longer want modules A and C to participate in the application anymore. If they have views registered with regions, I no longer want those views to be visible.

Developer
Jun 1, 2010 at 8:01 PM

Hi Simon,

Once a module has been loaded and initialized, it can’t be unloaded, as explained on a note in this article.

On the other hand, you can unregister views from regions and services from the container. The code for unregistering a view from a region would be something like this:

_regionManager.Regions["RegionName"].Remove(yourView);

If I understood your scenario correctly, you need to remove certain module’s views from a different module. A possible way to achieve this communication between modules would be to publish an event through the EventAggregator for the modules to known that they have to unregister their views. You can find more information on this on the following threads:

Membership in Prism

Login / Logout View Switching

I hope you find this helpful.

Guido Leandro Maliandi
http://blogs.southworks.net/gmaliandi

Jun 2, 2010 at 6:36 PM

Thank you. That information definitely helped. 

It looks like the above approach would work if using view-injection, but if you used view-discovery (i.e., IRegionManager.RegisterViewWithRegion()) I wasn't able to find a way to unregister a particular view with a region. I would have to use the view-injection approach. Thanks again. 

Developer
Jun 23, 2010 at 4:22 PM

Hi Simon,

That’s because View Discovery is generally not used when it is needed to remove a specific view from a region. In such cases you could use View Injection, which is better suited to this purpose. For more information, you can check this article (under the section “When to Use View Discovery vs. View Injection”).

I hope you find this helpful.

Guido Leandro Maliandi
http://blogs.southworks.net/gmaliandi