I wanted to start using the Model View ViewModel design pattern more often so I thought I would download a Visual Studio Template I could use when I create new projects. Well it seems like everyone is still a bit behind when it comes to creating templates for Visual Studio 2010. I did find a nice sample solution that contains all the basics. You can find it here http://coremvvm.codeplex.com/. This was good but I really didn’t want to have to go through editing each file every time I want to create a new project so I decided to try and export this as a Project Template
Exporting the project
- The first issue I had was that the “File > Export Template” option did not support exporting multiple projects. I did find a nice extension that allows you to do this though. You can download it from here or in Visual Studio 2010 go to Tools > Extension Manager and search online for “Export Template Wizard”
Once it is installed you will have an option in the file menu called “Export Template as VISX” but we need to make some modifications to the solution first so the template will use the project name we type when creating a new project. Visual Studio Templates use Template Parameters to replace parts of your projects with predefined variables. The only one I used was $safeprojectname$ but there are some other parameters that you might find useful such as $safeitemname$ if you’re creating an Item Template rather than a project template.
- This will break the solution and you won’t be able to build it anymore so create a copy first.
Change the Project Name and Solution Name to $safeprojectname$. I Left the second project as CoreMVVM but I think you can rename it to something like $safeprojectname$.CoreMVVM but I have not tried this
Normally the namespace would be the same name as what you had entered into the Add New Project dialog. To do this you need to replace any reference to the namespace with the $safeprojectname$ parameter. In this example I just replaced any reference to “WpfModelViewApplication2” to “$safeprojectname$ using the replace feature in visual studio. Make sure to replace the namespace in the xaml files and the in the AssemblyInfo.cs so it looks like this
Now save the changes and go to File > “Export File as VISX”. Select Project Template, check both projects and select next
Enter a Template Name, Description, Icon and Preview Image and select Next
Change the Product Name to the name of the Project Template and fill any out any other details. Then select Finish
Using the project template
- If you didn’t select the option “Automatically import the template into Visual Studio” on the last step double click on the .vsix file that was created to install it. This will then show in the Extensions Manager in visual studio
In Visual Studio select “New Project” and find the new template
Build the solution and the end result should look something like this. Note that the namespace has replaces any space characters with the underscore ( _ ) character