Professional Documents
Culture Documents
Prism Walkthrough
Prism Walkthrough
by geoffrey28. May 2010 09:20 The client side application is made out of three assemblies:
The foundation of the application is The Shell. The Shell is the top-level window of an application based on the Prism Composite Application Library. This window is a place to host different user interface (UI) components that exposes a way for itself to be dynamically populated by others, and it may also contain common UI elements, such as menus and toolbars. The Shell window sets the overall appearance of the application. In our application its the shell that is responsible to load the Modules. A module represents a set of related concerns. It can include components such as views, business logic, and pieces of infrastructure, such as services for logging or authenticating users. Modules are independent of one another but can communicate with each other in a loosely coupled fashion. In this walkthrough our application will contain only one module: the CoreModule nevertheless the modular architecture enable us to easily extend the application adding new modules later on. The Infrastructure Assembly is a shared library referenced by both the shell project and the module projects, and holds shared types such as constants, event types, entity definitions and interfaces.
Microsoft.Practices.Unity.dll Microsoft.Practices.ServiceLocation.dll
4. Add a reference to the Modules and Shell projects referencing the Infrastructure assembly. 5. Add a refrence to the Shell project referencing the Modules assembly. 6. Add a refrence to the Modules and infrastructure that reference to the WindowsBase & PresentationCore and PresentationFramework > Reference, Add, .Net tab. Your solution should look like this:
2. The Bootstrapper should inherit from Microsoft.Practices.Composite.UnityExtensions.UnityBootstrapper class and override the CreateShell method. You need to instantiate the Shell through the Unity conatiner and call his Show method
1: 2: 3: 4: 5: 6: 7: 8: 9: public class Bootstrapper : UnityBootstrapper { protected override System.Windows.DependencyObject CreateShell() { Shell shell = this.Container.Resolve<Shell>(); shell.Show(); return shell; } }
To be able to run the bootstrapper at least one module should be registered in our application. 1. Add a class to the PrismWalkthrough.Module and name it CoreModule.cs 2. This class should implement the IModule interface:
1: 2: 3: 4: 5: 6: 7: 8: 9: 10: namespace PrismWalkthrough.Modules { public class CoreModule : IModule { public void Initialize() { } } }
Through the bootstrapper you must configure which Modules are available in our Shell. You need to register at least one module in your bootstrapper. 1. Override the InitializeModules in the bootstrapper:
1: 2: 3: 4: 5: protected override void InitializeModules() { IModule coreModule = this.Container.Resolve<CoreModule>(); coreModule.Initialize(); }
The WPF Application class needs to execute the bootstrapper by calling his Run method.
Now you should be able to run you application, you should see a white screen.
Menu Region: contain a Menu that is responsible to load views in the Main Region Main Region: region that will load the workspace controls these are the views that will handle the application features. In our application only one view at a time can be loaded in the Main Region.
These regions are the placeholders for the controls defined in our Modules. Our application will contain only one Module: the CoreModule but this application can easily be extended by adding new modules. In our application the regions are defined as ItemsControls in the Shell.xaml file and can be accessed in a decoupled way by their names; they support dynamically adding or removing views at run time. 1. To add the regions in the Shell window add the following namespace definition to the root Window element. You need this namespace to use an attached property for regions that is defined in the Composite Application Library: xmlns:cal=http://www.codeplex.com/CompositeWPF. 2. Replace the Grid in the Shell by the following xaml:
3. <Grid> 4. <Grid.RowDefinitions> 5. <RowDefinition Height="35" /> 6. <RowDefinition Height="*" /> 7. <RowDefinition Height="35" /> 8. </Grid.RowDefinitions> 9. <ItemsControl Name="MenuRegion" cal:RegionManager.RegionName="MenuRegion" VerticalAlignment="Top" Grid.RowSpan="2" Height="26" HorizontalAlignment="Left" 10. Background="Transparent" Margin="0,12,0,0"/> 11. <Grid Margin="4,4,4,4" Grid.Row="1">
12.
<Border Background="GhostWhite" BorderBrush="LightGray" BorderThickness="1" CornerRadius="5" Margin="0,0,0,0"> 13. <ItemsControl Name="MainRegion" cal:RegionManager.RegionName="MainRegion" Margin="4" Height="291" /> 14. </Border> 15. </Grid> 16. <Border Grid.Row="2" > 17. <ItemsControl Name="StatusbarRegion" cal:RegionManager.RegionName="StatusbarRegion" Background="Transparent" /> 18. </Border> </Grid>
Now you should be able to run the application and see this screen:
CreateFileView: One of the views that can be displayed in the MainRegion CreatFolderView: Another view for the MainRegion MenuView: Control that will be responsible to Add views to the MainRegion StatusBar: Control that will be added to the StatusBarRegion
To add views to our regions we use the prism region manager service. The region manager service is responsible for maintaining a collection of regions and creating new regions for controls. Typically, we interact directly with region manager service to locate regions in a decoupled way through their name and add views to those regions. By default, the UnityBootstrapper base class registers an instance of this service in the application container. This means that we can obtain a reference to the region manager service in our application by using dependency injection. We use constructor dependency injection to gather an instance of the RegionManager and store it in a local field and its in the Initialize method of our CoreModule class that we implement logic to add the initial views to our Regions. 2) Modify the CoreModule class like:
1: public class CoreModule : IModule 2: { 3: private readonly IRegionManager regionManager; 4: 5: public CoreModule(IRegionManager regionManager) 6: { 7: this.regionManager = regionManager; 8: } 9: 10: public void Initialize() 11: { 12: this.RegisterViewsWithRegions(); 13: } 14: 15: protected virtual void RegisterViewsWithRegions() 16: { 17: this.regionManager.RegisterViewWithRegion(RegionNames.MenuRegion, typeof(MenuView)); 18: this.regionManager.RegisterViewWithRegion(RegionNames.StatusbarRegion, typeof(StatusbarView)); 19: } 20: }
To overcome magic constants in our code we used a class containing all our region names. 3) Add this RegionNames class to your Infrastructure Library:
1: namespace PrismWalkthrough.Infrastructure
2: 3: 4: 5: 6: 7: 8: 9:
{ public class RegionNames { public const string MainRegion = "MainRegion"; public const string MenuRegion = "MenuRegion"; public const string StatusbarRegion = "StatusbarRegion"; } }
Finally to see something displayed in our Menu and StatusBar we need to add some content inside these controls: 4) Modify the MenuView.xaml:
<StackPanel> <Menu Name="menu1" Background="Transparent" > <MenuItem Header="File" > <MenuItem Header="New" CommandParameter="CreateFile" /> <MenuItem Header="Folder" CommandParameter="CreateFolder" </MenuItem> </Menu> </StackPanel>
/>