Prism - region.add and regionManager.AddToRegion works differently

1k Views Asked by At

I was under the impression that the following two code statements are analagous

var region = regionManager.Region["SomeRegion"]
region.Add(container.Resolve<SomeView>());

and

regionManager.AddToRegion("SomeRegion", container.Resolve<SomeView>());

but as shown in my project, they both yield different results - what surprises me is that with regionManager.AddToRegion do not need to use Scoped Regions, but with region.Add we need scoped regions.

Please clone the repo, build and run the project, you should get an exception that ViewB is already registered. Now open TestModule.cs (see below) and comment "region.add" lines, and uncomment regionManager.AddToRegion, you will see the solution works perfectly.

I have gone through the docs and have seen projects intermittently using region.Add and regionManager.AddToRegion, but seems the API works differently.

https://github.com/rohits79/PrismRegionAmbiguity/blob/master/TestModule/TestModule.cs#L22-29

Update

Noticed that if i add three lines of AddToRegion then application blows up

regionManager.AddToRegion("ViewA", unityContainer.Resolve<ViewA>());

however it takes only two lines of

region.Add(container.Resolve<ViewA>());

Does this not look like an issue in Prism, wonder why AddToRegion works without creating scope?

1

There are 1 best solutions below

1
AudioBubble On

I've seen this in the past. I never took the time to look into it because it's not that big of a deal. AddToRegion actually calls Region.Add, so I'm not really sure why this would happen off the top of my head. I think this depends on the Region type, because calling region.Actiavte will invoke the error immediately.

Just so you know, if you have multiple instances of a region you MUST ALWAYS use scoped regions.