Reputation: 12804
I'm new to Prism, and I'm currently writing my first application using Prism's "region" concept. I currently have a Shell
application that contains a single region, into which I display a view defined in a separate module. This view has two of its own regions, Subregion1
and Subregion2
, which display two views defined in the module.
This is all working correctly, and the view and subviews are displaying in the proper location. However, when I attempt to find one of the subviews in any of the view's or subview's viewmodels (using regionManager.Regions["Subregion1"];
), I am greeted with the error:
The region manager does not contain the SiteManager.SiteList region.
I initially thought that this might be a problem of having multiple instances of IRegionManager
s, but most of the documentation I've read indicates that the global instance of IRegionManager
is provided unless a scoped IRegionManager
is specifically requested.
I've also tried various combinations of the static RegionManager.UpdateRegions()
and RegionManager.SetRegionManager( .... )
, to no avail.
Any ideas as to why the RegionManager
isn't able to find the regions I'm requesting, even though I can register views to these regions without any problems?
EDIT: I'm attempting to access the regions in a command in the subviews's viewmodels.
Upvotes: 1
Views: 1816
Reputation: 1288
I know it is an old question but I hit the same problem so I'll add my finding for someone hit the same problem in future.
In the current Prism implementation, default region manager is stored as an attached dependency property to the shell window.
In Prism 7.2, region manager is set to shell window in the application base class.
public virtual void Initialize()
{
...
var shell = CreateShell();
if (shell != null)
{
RegionManager.SetRegionManager(shell, _containerExtension.Resolve<IRegionManager>());
RegionManager.UpdateRegions();
InitializeShell(shell);
}
InitializeModules();
}
Though it's a singleton it's not automatically inherited to sub windows including dialogs created by DialogService
. So if you want to use region in dialogs, you have to attach region manager manually.
There are a few questions basically related to the same question.
Here is another example to set region manager to dialog.
public class SampleDialogViewModel {
public IRegionManager RegionManager { get; }
public SampleViewModel(IRegionManager regionManager)
{
RegionManager = regionManager;
}
....
protected override void OnDialogClosed(IDialogResult result)
{
// You also have to manually remove region when dialog is closed.
RegionManager.Regions.Remove("MyRegion");
}
}
And in your view, you can use Prism attached property to bind region manger to an element.
<ContentControl Grid.Row="0"
prism:RegionManager.RegionName="MyRegion"
prism:RegionManager.RegionManager="{Binding RegionManager}"/>
Also please note that if you want to create modeless dialogs, you probably need to create sub regions for each dialog window, but that's another problem. I hope this will be more easily handled in future versions.
Upvotes: 1
Reputation: 25201
The reason you're getting this exception is because you're trying to access the regions from the view's/viewmodel's constructor, where they haven't been created yet. You could easily verify that by trying to access them from the view's Loaded
event, where they've already been initialized.
Any ideas as to why the RegionManager isn't able to find the regions I'm requesting, even though I can register views to these regions without any problems?
Registering views to regions doesn't require the region to actually be loaded. When you register a view to a region, that information is saved in memory, and when the region is created (which can be much later), Prism knows to initialize that region with the appropriate views.
Upvotes: 1