To get started with your own CompositeWPF app, here's a quick cheat-sheet of steps. You can reference the demo code as well, to see how each step was implemented.
- Download and build the libraries from http://www.codeplex.com/compositewpf
- Start a new WPF application project
- Add references to either the built CompositeWPF libraries or the actual project files (it's probably more efficient to just reference the built libraries, in a common place)
- Delete Window1.xaml and create a new WPF Window called Shell.xaml
- Add the http://www.codeplex.com/CompositeWPF namespace to the namespaces in Shell.xaml
- Create one or more named regions in Shell.xaml.
- In App.Xaml, remove StartupUri (remember, we'll be loading the appropriate window with the bootstrapper).
- Create a new class, called Bootstrapper. Derive from UnityBootstrapper.
- In Bootstrapper, override CreateShell(): instantiate, show, and return the Shell window.
- In App.Xaml.cs, override OnStartup(). Create a new Bootstrapper object and call its Run() method.
- Add a module to your solution. This is a regular class library. Name it
Module. Make sure the class implements IModule, which requires the Initialize() method. - Create a view by adding a WPF user control to your new module. Start off simple, and just add something like a TextBlock so you can see your view later.
- Add an IRegionManager parameter to the constructor (which will be filled in by the Unity Container). Save it off in a member variable.
- Have the Initialize() method call RegisterViewsAndServices() (a private method on your module). In RegisterViewsAndServices(), register your module's view with the RegionManager, and assign it to one of your named regions.
- Now that your module exists, go back to your main app's project (ours was the Demo project). Add a reference to your new module. Then, in Bootstrapper.cs, override GetModuleCatalog(), and create a new catalog with your new module.
That should do it - when running your app, you should see your view appear in your app's window, wherever you placed the named region. For our demo app, we used a grid with three regions aligned row by row.
For any questions regarding the demo code, please leave a comment here.
Thanks for attending!


1 comments:
Hi David,
Thanks for posting your demo code from the Northern Va. Code Camp presentation! I was able to open the project, add the appropriate references to the CAL and I got the demo to run.
Quick question: how do the Views get associated with the ViewModels during runtime? (Is that done by the omnipetent unity?) Let's take the PersonDetailsModule specifically. I see in the code-behind file for PersonDetailsView.xaml.cs that the PersonDetailsView constructor takes a PersonDetailsViewModel as a parameter and sets it as the data context for the view. But "who" actually calls the PersonDetailsView constructor, and how does "it" know how to instantiate a PersonDetailsViewModel and pass it into the PersonDetailsView constructor?
I'm curious as to how this works because I want to try extending a module to have more than one view and possibly more than one viewmodel -- and understand the lifetime of the view and viewmodel instances.
I appreciate any insight you can offer.
Thanks much,
Ken
Post a Comment