简体   繁体   中英

Composite view - View already exists in region error

I am working on a WPF/Prism 4/AvalonDoc application.

One of my views is very complicated and a ViewModel become huge and unmanagable. So I decided to split it to smaller views with their own ViewModels.

I have placed a region on the view and loading the sub-view into that region using regionManager.RegisterViewWithRegion method from "master" ViewModel constructor.

That works just as expected when only one instance of a view is loaded. But when I am opening second instance of that screen (it is loaded as DocumentContent into AvalonDock's DocumentPane) I am getting "View already exists in region" error.

I would like to avoid giving the region some unique name if possible. Is there a way to indicate that I am adding sub-view to specific instance of the "master" view? Any advise on how to do this?

If you are using MEF and your view is Singleton ie

[PartCreationPolicy(CreationPolicy.Shared)]

and your INavigationAware return false on

public override bool IsNavigationTarget(NavigationContext navigationContext)

{return false;}

You will get this error.

Change [PartCreationPolicy(CreationPolicy.Shared)] to [PartCreationPolicy(CreationPolicy.NonShared)]

and you are done.

I fear that you cannot have several regions with the same name. I would prefer not to split the views when the view model gets complicated. You can split the view model into several specialized view model.

An example. You have a user management. One view model is the list of all users. This list contains user view models. And each user view model contains an instance of a user rights view model which itself is again a list of view models that represent only one user right. So the logic on how to present a special user right goes into the correspondent view model and not into the "master" user list view model. This way you can separate your concerns into different view models.

Binding to those view models is just as simple as having only one view model. Just the path is getting a bit longer. When the data context is the userlist view model from the example above you could propably have a binding path like CurrentUserVM.UserRightsVM to get the list of user rights.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM