Visual Studio 2010, as its
predecessors since 2005, enables exporting projects and items templates.
This feature is useful because you can generate skeletons for
applications or single items that you can reuse in the future. This
section covers both options, starting from project templates.
Exporting Project Templates
Imagine you often
create WPF applications that need implementing menus. It would be
interesting to have a project template that automatically implements
basic menus so that you don’t need to rewrite a lot of code each time.
Create a new WPF project with Visual Basic and name it as WpfMenuProject. When ready, type the following XAML code inside the Grid tags to implement basic menu functionalities:
<DockPanel LastChildFill="True" VerticalAlignment="Top">
<Menu DockPanel.Dock="Top">
<MenuItem Header="File" IsEnabled="True"
DockPanel.Dock="Top">
<MenuItem Header="_Open" Name="Open"/>
<Separator/>
<MenuItem IsEnabled="True" Name="Save">
<MenuItem.Header>_Save</MenuItem.Header>
</MenuItem>
<MenuItem IsEnabled="True" Name="Exit">
<MenuItem.Header>_Exit</MenuItem.Header>
</MenuItem>
<Separator />
</MenuItem>
<MenuItem Header="Edit"
DockPanel.Dock="Top">
</MenuItem>
</Menu>
</DockPanel>
Before going on, save the project. When saved, select the File, Export Template command. The first choice is between the project and item template, as shown in Figure 1. Leave unchanged the selection on Project Template and then click Next.
Visual Studio now asks you to
enter some information on the new template, such as the name, the
description, and optionally an icon image and a preview image. This
information will be shown in the New Project dialog. Assign the
WpfMenuProject name as the template name and provide a custom
description; you can also add images but this is not mandatory. (Visual
Studio will provide a default image for you.) Figure 2 shows how the dialog appears.
Projects templates are
simply .zip packages containing the project skeleton and an information
file named MyTemplate.vstemplate. By default, custom templates are
exported to the %UserProfile%\Documents\Visual Studio 2010\My Exported
Templates folder, and by default they are automatically imported into
the IDE (although you can decide to remove this option). When you click Finish, the new template is available in Visual Studio. You can check this out by selecting the File, New Project command. Figure 3 shows how the new template is available in the New Project dialog.
Now you can create a new project
based on the custom template without the need of rewriting code that is
already exposed by the template.
Exporting Item Templates
You can
export single items, such as classes or controls, to item templates
that you can later add to other projects. To provide an example, add a
new class to the current project and name it as DisposableClass.vb. The goal of the example is to provide a template for classes implementing the IDisposable interface. When ready, simply implement the IDisposable
interface in the new class so that Visual Studio will add required
members. Now select again File, Export Template. In the first dialog of the wizard, select the Item Template option (as shown in Figure 4) and then click Next.
The next step is selecting the
item you want to be exported as a reusable template. Notice that you
can choose multiple items. Select the DisposableClass.vb item, as shown in Figure 5; then click Next.
The next step is selecting
required references so that when you add an item based on the new
template Visual Studio will reference the necessary assemblies for you. Figure 6 shows how the dialog appears.
In this case the
template requires nothing but default assemblies, so no references are
required. The final step is where you provide information to the custom
item template. Basically such information is the same as for project
templates. Assign Template Name with DisposableClass, provide a description (see Figure 7 for details), and eventually provide icon and preview images. Finally click Finish.
When the export process has
been completed, the new template is available in Visual Studio. With a
project open, right-click the project name and select Add New Item. When the export process has been completed, the new item template is available in Visual Studio 2010, as demonstrated by Figure 8, which represents the Add New Item dialog.
With a
few steps you can produce your custom project and item templates, which
can help you by saving time when you repetitively create the same
project types.