Prism Automation: Templates for implementing the MVVM pattern using the Composite Application Library1 Comment
With the help of Damian Schenkelman and Julian Dominguez, we created some Visual Studio Item Templates that provides the basic assets for implementing a new view using the MVVM pattern in Prism. These templates create the necessary files and classes for the view and viewModel parts of the pattern.
I will explain in this post how to install and use the Prism MVVM templates. If you are not familiar with the MVVM pattern the following links are a good staring point:
- Model View ViewModel (MVVM) in the prism Knowledge Base.
- WPF Apps With The Model-View-ViewModel Design Pattern
- Model-View-ViewModel In Silverlight 2 Apps
- DataModel-View-ViewModel pattern: 1
- WPF patterns : MVC, MVP or MVVM or…?
These MVVM templates aim to help you facilitate the creation of views with presenters when developing prism applications. Then we disscuss briefly some implementation considetarions.
David Hill has recently posted the Prism Quick Start Kit which includes project templates for creating prism Modules and Shell, both for WPF and Silverlight. Damian has created code snippets for easing the creation of Commands with attached Behaviors.
How-To: Install the Prism MVVM Templates
Installing the templates it’s really simple!
- Dowloand templates and unzip them into your Visual Studio templates folder. They are C# templates so they go here on my system:
My Documents\Visual Studio 2008\Templates\ItemTemplates\Visual C#
- To avoid getting a warning message the first time you use them, you can close Visual Studio and run the following command (as admin) in a Visual Studio Command Prompt window:
That’s all! you should now be able see them in the New Items dialog.
How-To: Use the Prism MVVM Templates
Using the templates it’s also really simple (that’s the idea of using templates after all :-)).
- Open any prism application in Visual Studio. (Templates will work in any kind of application, but you will have to reference CAL assemblies).
- Right click on the project/folder where you want to place your new view and select Add / New Item…The New Item Dialog will appear.
- Select Prism MVVM or Prism MVVM (with interfaces) from the My Templates category.
- Click Add. This will create the View with xaml and code behind, and the ViewModel. If you selected Prism MVVM (with interfaces) template, it will additionally create interfaces for the View and ViewModel (this is highly recommended as it will ease testing).
Great, we have now the new view with it’s corresponding ViewModel. To show the view you should register it in the appropriate region as usual. For example, using view discovery (if you didn’t used interfaces, you won’t need the first line):
this._container.RegisterType<ICustomersViewModel, CustomersViewModel>(); this._regionManager.RegisterViewWithRegion(RegionNames.ButtomLeftRegion, () => this._container.Resolve<CustomersView>());
Using the above code in David’s Quickstart Solution, you can get the view shown:
You will notice in the implementation of the view and viewModel that:
- View-First approach is used.
- Templates can be used in WPF and Silverlight prism applications.
- The implementation relies on prism, as it inyects the EA to the ViewModel.
- A property is created in the viewModel, to demostrate databinding in the view.
You can download the templates from here.