Patrik B
Patrik B

Reputation: 151

How display multiple instances of a view using Prism and Unity

I'm working on a .Net application using WPF, Unity and Prism 4. The application will consist of a shell with multiple views on tab pages. The application relies heavily on a module named SystemTreeModule that contains the SystemTreeView which will appear in many places.

I have just finished of my first view where I defined a region named "SystemTreeRegion". In the SystemTreeModule I registered an instance of my SystemTreeView with that region. Works like a charm! The SystemTreeView type is discovered using Unity's autodiscovery-feature (so it is not registered explicitly):

public void Initialize()
{
    _regionManager.RegisterViewWithRegion("SystemTreeRegion", () => _container.Resolve<SystemTreeView>());
}

Now it's time to start the work on the second module where I want to use the SystemTreeView. I felt really confident when I created a region named "SystemTreeRegion" in the new view, but it fails with the following exception:

An exception occurred while creating a region with name 'SystemTreeRegion'. The exception was: System.ArgumentException: Region with the given name is already registered: SystemTreeRegion

So I have both googled and searched StackOverflow for answers but I haven't been able to figure out how you are supposed to do this!

I can't even tell if I'm just missing some detail or if I'm conceptually way off...

Upvotes: 2

Views: 1919

Answers (1)

Spontifixus
Spontifixus

Reputation: 6660

You are not way off conceptually. Providing a view by means of a module is quite OK. But keep in mind that you can have only one view named "SystemTreeRegion" in your application, because otherwise Prism's region manager won't know which view you want to access.

Taking that into account - you are conceptually wrong if you try to register a view to a region's name and hope that the view will appear in every region carrying that name. You can only register views to a region.

So simply ensure that every region has its own unique name, and then register the view you need for that region and you will be fine.

Upvotes: 1

Related Questions