If you've been following the blog for any length of time you'll know how much I love Visual Studio Template.
What? You're new'ish here? Well here's a list of some of my Template related posts;
- Visual Studio Extension that helps you create Visual Studio Extensions
- Intense UWP Templates with the Intense Toolkit and some Logo help
- Turning your Windows 10 Project Templates up to 10 with Template10!
- Go beyond "Blank App" with these Windows 10 VS 2015 Project Templates
- Extending VSTS with Project Template, Docs and more...
- Project, project, project, creating multi-project Solutions
- No waffling here... SideWaffle and a step-by-step guide to create Visual Studio Item Templates
- Windows IoT Core Visual Studio Project Templates
- Adding Magic to New Projects with the VSX Template Wizard
Yeah, just a few there... ;)
An today's post in going to pile on some more.
First Damir Arh shows us how we can create and share Visual Studio Project Templates, then Donald Kackman provides a great example of why you'd want to do this and finally Brian Lagunas shares the just release Prism Template Pack.
Usually, there is no need to create your own Visual Studio project template. The existing ones should suffice for most cases. However, if you find yourself often creating new projects and manually adding the same files or references to them; creating a new project template might make sense. It will save you time and prevent minor variations between the projects.
This article is published from the DNC Magazine for .NET Developers and Architects. Download this magazine from here [Zip PDF] or Subscribe to this magazine for FREE and download all previous and current editions
Preparing the Desired Project
The starting point for any new project template is another Visual Studio project, structured exactly as you would like a new project to look like, when you create it. As an example, we will prepare a new class library project, which you could use for trying out small code snippets to answer questions in forums or solve programming exercises. It will contain two classes: one for the sample code and one for the tests. NUnit unit testing framework will be referenced as a NuGet package.
I will be using Visual Studio 2015 for all my samples. Unless mentioned otherwise, the experience is the same in Visual Studio 2013. Just replace 2015 with 2013 wherever it appears in the text. You can use Visual Studio Community Edition or any of the other editions, except Express.
Here are the steps for creating such a project:
Exporting the Project as a Template
The easiest way to create a project template is by using the Export Template Wizard that is built into Visual Studio. Just navigate to File > Export Template… to open it. On the first page you can choose between creating a project template or an item template (Project template in our case), and select the project you want to create the template from (Sample.Project in our case).
On the second page, you will have a chance to enter the name and description of the template, and select the optional icon and preview image. You can keep both options checked to automatically import the template (i.e. copy it to Visual Studio 2015\Templates\ProjectTemplates inside My Document folder) and open the output directory (as shown in the wizard).
Creating a Project Template Manually
Once you have Visual Studio extensibility tools installed, there is a Visual Studio project template available for creating customized project templates without using the Project Template Wizard. It is named C# Project Template and can be found in the Visual C# > Extensibility node of Add New Project Dialog. Create a new project from this template in your solution and name it Sample.Template.
A couple of files inside the created project require a closer look.
Packaging the Template as a Visual Studio Extension
To wrap the project template inside a Visual Studio extension, you will need to add another project to your solution. This time use the VSIX Project template (also located in the Visual C# > Extensibility node) and name it Sample.VSIX.
Now double click the source.extension.vsixmanifest file in the newly created project to open the editor window. Navigate to the Assets page and add Sample.Template as a new project template asset.
The tooling for creating custom Visual Studio project templates is descent, but except for reference documentation in MSDN, there are not many resources about it. Even most of those were written for older versions of Visual Studio. The primary goal of this guide is to make you consider project template creation as another tool in your tool belt. If you decide to use it, this guide should be more than enough to get you started.
This is a VS.NET project template that creates the skeleton of a Windows 10 Universal app with these features already wired in:
- MVVM Light scafolding
- Navigation bar like the Microsoft built in apps (Weather, Groove Music etc)
- Navigation is wired to the built in Back button
- A setting page (and matching Model and ViewModels) with
1. Persitent Theme chooser
2. Credits page
3. About page
I hate doing this stuff over and over again so there ya go...
If you develop applications using Prism, there have probably been a number times where you said, “Man, I wish I could just click File –> New –> Prism Application, and have Visual Studio create all that monkey code for me”. I mean, let’s face it, a lot of the the code you write to setup a Prism app is pretty repetitive. Not much really changes when setting up a new app. A lot of times you have to add very common items to an existing app, which may be views that you need to add the ViewModelLocator attached property, adding a ViewModel that implements BindableBase, or even adding a new module.
Well, I am happy to announce, that now you can automate all that setup code right within Visual Studio. We have released a new VSIX to the Visual Studio Gallery called the Prism Template Pack. I actually released the Prism Template Pack about a month back as a v1 just to get some feedback from the community before writing a blog about it. Well, the feedback has been great, and now we are at v1.3. What do you get with the new Prism Template Pack? Well, it depends on the platform.
Let’ take a look at what you get in the current 1.3 version of the Prism Template Pack.
Snippets are a core concept in Visual Studio and have been for a long time. I use them constantly, and they really save time stubbing out a lot of verbosity in your code very quickly. We have recognized three snippets of code that are used the most in every Prism application.
Item templates are probably the most used item in Visual Studio, and you may not even realize it. Ever time you add a new item to your project inside Visual Studio, you are using an item template. The Prism Template Pack provides a number of item templates depending on the platform.
Every platform will have access to the Prism ViewModel item template. This item template will create a new class that derives from Prism.Mvvm.BindableBase with an empty default constructor. When you add tis item to your project, this is the class that will be created
Now, when building a WPF Prism application there are two major items that we identified that should be an item template.
- Prism UserControl – this will create a WPF UserControl with the Prism namespace defined as well as the ViewModelLocator.AutowireViewModel attached property set to true.
- Prism Window – this item will create a WPF Window with the Prism namespace defined as well as the ViewModelLocator.AutowireViewModel attached property set to true.
I think one of the biggest benefits of the Prism Template Pack item templates comes to the Xamarin.Forms platforms. That’s because Xamarin’s item template suck! Hell, when you select a “XAML Page”, the default file name ends in .CS and not .XAML. How hilarious is that? So to help speed up the creation of the various pages types in Xamarin.Forms, the Prism Template Pack provides these item templates.
- Prism ContentPage – ContentPage with ViewModelLocator
- Prism NavigationPage – NavigationPage with ViewModelLocator
- Prism MasterDetailPage – MasterDetailPage with ViewModelLocator
- Prism TabbedPage – TabbedPage with ViewModelLocator
- Prism CarouselPage – CarouselPage with ViewModelLocator
Project templates are used to get your project started or to add additional assemblies to a current project in your solution. I would like to point out that Project Templates aren’t meant to be full blown sample app with a lot of code and functionality added to teach you how to use something. They are only meant to get you up and running with the bare minimum code required without making too many assumptions about how you are going to architect or write your app.
For WPF we have two project templates.
- Prism Unity App – this is a project template that essentially creates a new WPF shell application and uses Unity as the container. It will have a basic bootstrapper that is responsible for initializing the app, and showing the shell. It will have a MainWindow and a MainWindowViewModel located in the Views and ViewModels folders respectively.
- Prism Module – this project template will add a new project to your solution that will act as a Prism module. It will have a class defined that implements IModule with two empty folders for your Views and ViewModels.
Since Xamarin.Forms doesn’t have the concept of modularity (not yet anyways), there is only one project template.
- Prism Unity App – this project template will create a Xamarin.Forms application with four projects; a PCL project for shared code, an iOS app, an Android app, and a Windows Phone app that all use Unity as the conatiner.
Last but not least, we have editors. By that, I mean custom editors written specifically to help you be more productive creating your Prism applications. I am actually the most excited about this one, because I got to learn something new in order to create it. So far I only have one editor, and that is the new App.config Prism Module Editor. Hand writing XML in your App.config can be a pain in the ass, and it’s not type safe so it is extremely easy to get the syntax wrong, or introduce any number of typos that will cause your app to fail to load your modules and flat out crash.
Since WPF is the only platform that supports having an App.config file act as a module catalog, it is only useful in WPF. In order to use it simply right-click your App.config file, and select Open With Prism Module Editor.
Let’s Wrap this Baby Up!
I hope you find these snippets, item templates, project templates, and custom editors useful. If you have any ideas or suggestions, please feel free to send them my way. Also, keep in mind that this was my first attempt at creating templates and a custom editor, so if you find any issues be gentle. I am learning as I go and could really use your help to improve them. Be sure to report any issues you run into, and feel free to fix them and submit your PR.
Comments have been closed since this content was published more than 30 days ago, but if you'd like to send us feedback you can Contact Us.