If you use the prism framework, read this article about scoped regionmanager. The prism framework has regionmanager. You can use iregionmanager injection to obtain the global regionmanager. Today, we are not talking about the global regionmanager, but the scoped regionmanager, that is, the regionmanager in the scope. What does it mean? If your application contains complex region nesting, for example, your application has a large tabregion, and each time you open a tab, a view is injected and each view contains tabregion, each time a tab is opened, a view is injected, and each view contains tabregion ..... In this case, region is nested in the composite mode. If you close a tab and open another tab, or open multiple tabs of the same type, the following exception occurs:
An exception occurred while trying to create region objects.
-The most likely causing exception was: 'System. reflection. targetinvocationexception: exception has been thrown by the target of an invocation. --->
Microsoft. Practices. Prism. Regions. behaviors. regioncreationexception: An exception occurred while creating a region with name subtabsubregion.
The exception was: system. argumentexception: Region with the given name is already registered: subtabsubregion
At Microsoft. Practices. Prism. Regions. regionmanager. regioncollection. Add (iregion Region)
At Microsoft. Practices. Prism. Regions. behaviors. regionmanagerregistrationbehavior. tryregisterregion ()
At Microsoft. Practices. Prism. Regions. behaviors. regionmanagerregistrationbehavior. startmonitoringregionmanager ()
At Microsoft. Practices. Prism. Regions. behaviors. regionmanagerregistrationbehavior. onattach ()
At Microsoft. Practices. Prism. Regions. regionbehavior. Attach ()
At Microsoft. Practices. Prism. Regions. regionbehaviorcollection. Add (string key, iregionbehavior regionbehavior)
At Microsoft. Practices. Prism. Regions. regionadapterbase '1. attachdefadefabehaviors (iregion region, t regiontarget)
At Microsoft. Practices. Prism. Regions. regionadapterbase '1. initialize (T regiontarget, string regionname)
At Microsoft. Practices. Prism. Regions. regionadapterbase '1. Microsoft. Practices. Prism. Regions. iregionadapter. initialize (Object regiontarget, string regionname)
At Microsoft. Practices. Prism. Regions. behaviors. Attributes. createregion (dependencyobject targetelement, string regionname). ---> system. attributes: Region with the given name is already registered: subdevicesregion
At Microsoft. Practices. Prism. Regions. regionmanager. regioncollection. Add (iregion Region)
At Microsoft. Practices. Prism. Regions. behaviors. regionmanagerregistrationbehavior. tryregisterregion ()
At Microsoft. Practices. Prism. Regions. behaviors. regionmanagerregistrationbehavior. startmonitoringregionmanager ()
At Microsoft. Practices. Prism. Regions. behaviors. regionmanagerregistrationbehavior. onattach ()
At Microsoft. Practices. Prism. Regions. regionbehavior. Attach ()
At Microsoft. Practices. Prism. Regions. regionbehaviorcollection. Add (string key, iregionbehavior regionbehavior)
At Microsoft. Practices. Prism. Regions. regionadapterbase '1. attachdefadefabehaviors (iregion region, t regiontarget)
At Microsoft. Practices. Prism. Regions. regionadapterbase '1. initialize (T regiontarget, string regionname)
At Microsoft. Practices. Prism. Regions. regionadapterbase '1. Microsoft. Practices. Prism. Regions. iregionadapter. initialize (Object regiontarget, string regionname)
At Microsoft. Practices. Prism. Regions. behaviors. delayedregioncreationbehavior. createregion (dependencyobject targetelement, string regionname)
--- End of inner exception stack trace ---
At Microsoft. Practices. Prism. Regions. behaviors. delayedregioncreationbehavior. createregion (dependencyobject targetelement, string regionname)
At Microsoft. Practices. Prism. Regions. behaviors. delayedregioncreationbehavior. trycreateregion ()
At Microsoft. Practices. Prism. Regions. behaviors. delayedregioncreationbehavior. onupdatingregions (Object sender, eventargs E)
--- End of inner exception stack trace ---
At system. runtimemethodhandle. _ invokemethodfast (iruntimemethodinfo method, object target, object [] arguments, signaturestruct & Sig, methodattributes, runtimetype typeowner)
At system. reflection. runtimemethodinfo. Invoke (Object OBJ, bindingflags invokeattr, binder, object [] parameters, cultureinfo culture, Boolean skipvisibilitychecks)
At system. Delegate. dynamicinvokeimpl (object [] ARGs)
At system. Delegate. dynamicinvoke (object [] ARGs)
At Microsoft. Practices. Prism. Events. weakdelegatesmanager. Raise (object [] ARGs)
At Microsoft. Practices. Prism. Regions. regionmanager. updateregions ()'.
To solve this problem, scoped region is used. For example, when detailsview is opened, iregionmanaer r = region is used. add (view, key, true) method, and then use the newly created scoped regionmanager instance to open a new view, instead of using global regionmanager.Code:
1: // Pay attention to the return value. Pay attention to the last parameter.
2:Iregionmanager detailsregionmanager =
3:Detailsregion. Add (detailspresenter. view,
4:Employee. employeeid. tostring (cultureinfo. invariantculture ),True);
5:
6: // Use the new scoped regionmanager instance to add a view!
7:Iregion region = detailsregionmanager. Regions [regionnames. tabregion];
8:Region. Add (projectslistpresenter. view,"Currentprojectsview");
9:Detailsregion. Activate (detailspresenter. View );
If you use the mvvm mode for development, and each time you open a new view, it is an independent mvvm mode, then the problem arises. How does scoped regionmanager pass to the viewmodel of the new view? This isHow to transmit data transmission parameters between viewmodels. I expanded regionmanager using the extension method. add the complexdata parameter to the requestnavigate method, so that I can put iregionmanager (scoped regionmanager) IN THE complexdata parameter and use regionmanager. the requestnavigate method is passed to the viewmodel of the newly created view. It is completely a parameter transmitted between two viewmodels. For more information about the code, see my other blog.
For other scoped regionmanager resources:
- msdn: Region
- How to: show a view in scoped region