Reputation: 15441
I've (very) recently started studying about Prism (for a WPF app) and I've been working on a small demo app for my team. My question is a rather general one but I can't find a simple example to direct me:
Assuming I have the Shell (in the main module), and the Shell has a region which should be filled by a content from a different module (BTW, is this a good idea?).
How does this happen exactly? Who's responsible for adding the view from the other module into the Shell's designated region? When is the other module's relevant view-model being initialized? Code samples / links to them would be appreciated.
Thanks!
Edit: Question split, please see the new question.
Upvotes: 1
Views: 754
Reputation: 11760
Yes, separating shell and views is a good idea.
The RegionManager is responsible for adding the view into the regions. Therefore you register the region to the RegionManager as well as you register the view to the RegionManager.
Inside the shell you will end up with something like:
<Window xmlns:Regions="clr-namespace:Microsoft.Practices.Prism.Regions;assembly=Microsoft.Practices.Prism"
xmlns:Inf="clr-namespace:YourNamespace.Infrastructure;assembly=YourNamespaceInfrastructure">
<Grid>
<ContentControl
Regions:RegionManager.RegionName="{x:Static Inf:RegionNames.MainRegion}"/>
</Grid>
</Window>
Now you have to register the view to the region it will reside in:
public class FirstModule : IModule
{
public void Initialize( )
{
RegionManager.RegisterViewWithRegion( RegionNames.MainRegion,
typeof( MainView ) );
RegionManager.RegisterViewWithRegion( RegionNames.SecondRegion,
( ) => Container.Resolve<ISomethingViewModel>( ).View );
}
}
The example code shows both types of registrations. The first one is for view first approaches, the latter on for view model first approaches.
[EDIT]
The region names are defined in a static class in the infrastructure module:
namespace YourNamespace.Infrastructure
{
public static class RegionNames
{
public const string MainRegion = "MainRegion";
//...
}
}
[/EDIT]
Upvotes: 1
Reputation: 75
In your bootstrapper you would define a module catalog (overriding CreateModuleCatalog) which lists the modules that will be used within your application. Each module has a class implementing IModule which contains an Initialize method that gets called when the module is being created. Within this you would define what is to be added to the region on the shell:
public void Initialize()
{
RegisterViewsAndServices(); //Method to register dependencies
IMyViewModel model = _container.Resolve<IMyViewModel>();
_regionManager.Regions[RegionNames.ShellHeaderRegion].Add(model);
}
Here I have added a viewmodel to the shell and I have a resource dictionary defined outside that determines what view should be applied to it via a DataTemplate.
The Prism documentation (particularly chapter 2) has a lot of useful information around this. http://msdn.microsoft.com/en-us/library/gg406140.aspx
Upvotes: 2