Visual Studio 2010 IDE : Exporting Templates

9/4/2012 9:15:22 PM
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"

            <MenuItem Header="_Open" Name="Open"/>
            <MenuItem IsEnabled="True" Name="Save">
            <MenuItem IsEnabled="True" Name="Exit">
            <Separator />

        <MenuItem Header="Edit"

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.

Figure 1. Exporting a project template.

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.

Figure 2. Setting information about the new template.

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.

Figure 3. The New Project dialog includes the new template.

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.

Figure 4. Setting the Item template option.

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.

Figure 5. Adding an item to the new template.

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.

Figure 6. Specifying references for the new item template.

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.

Figure 7. Providing custom information to the new item template.

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.

Figure 8. The Add New Item dialog shows the new item template.

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.

  •  System Center Configuration Manager 2007 : Certificate Requirements Planning, Windows Server 2008 Planning
  •  System Center Configuration Manager 2007 : Planning for Internet-Based Clients
  •  Active Directory Domain Services 2008 : Automatically Populate a Migration Table from a Group Policy Object
  •  Active Directory Domain Services 2008 : Create a Migration Table
  •  Microsoft Content Management Server : Developing Custom Properties for the Web Part
  •  Microsoft Content Management Server : Building SharePoint Web Parts - Creating the Web Part, Defining Custom Properties for the Web Part
  •  Microsoft Content Management Server : Building SharePoint Web Parts - The SharePoint MCMS Navigation Control, Creating the Web Part Project
  •  Active Directory Domain Services 2008 : Search Group Policy Objects
  •  Active Directory Domain Services 2008 : Export a Starter GPO, Import a Starter GPO
  •  The Very Successful Hardware That Microsoft Has Ever Produced
  •  Xen Virtualization - Managing Xen : Virtual Machine Manager
  •  Xen Virtualization - Managing Xen : XenMan—Installing and Running
  •  Dual-Core Or Quad-Core?
  •  IBM WebSphere Process Server 7 and Enterprise Service Bus 7 : Getting Started with WID (part 2) - Working with Modules and Libraries
  •  IBM WebSphere Process Server 7 and Enterprise Service Bus 7 : Getting Started with WID (part 1) - Business Integration perspective
  •  Google vs. Apple vs. Microsoft
  •  DSLs in Boo : Implementing the Scheduling DSL
  •  DSLs in Boo : Combining domain-driven design and DSLs
  •  Active Directory Domain Services 2008 : Restore Group Policy Objects and Starter GPOs
  •  Active Directory Domain Services 2008 : Comment Group Policy Objects and Starter GPOs, View, Print, and Save a Report for Group Policy Objects, Back Up Group Policy Objects and Starter GPOs
    Top 10
    Nikon 1 J2 With Stylish Design And Dependable Image And Video Quality
    Canon Powershot D20 - Super-Durable Waterproof Camera
    Fujifilm Finepix F800EXR – Another Excellent EXR
    Sony NEX-6 – The Best Compact Camera
    Teufel Cubycon 2 – An Excellent All-In-One For Films
    Dell S2740L - A Beautifully Crafted 27-inch IPS Monitor
    Philips 55PFL6007T With Fantastic Picture Quality
    Philips Gioco 278G4 – An Excellent 27-inch Screen
    Sony VPL-HW50ES – Sony’s Best Home Cinema Projector
    Windows Vista : Installing and Running Applications - Launching Applications
    Most View
    Bamboo Splash - Powerful Specs And Friendly Interface
    Powered By Windows (Part 2) - Toshiba Satellite U840 Series, Philips E248C3 MODA Lightframe Monitor & HP Envy Spectre 14
    MSI X79A-GD65 8D - Power without the Cost
    Canon EOS M With Wonderful Touchscreen Interface (Part 1)
    Windows Server 2003 : Building an Active Directory Structure (part 1) - The First Domain
    Personalize Your iPhone Case
    Speed ​​up browsing with a faster DNS
    Using and Configuring Public Folder Sharing
    Extending the Real-Time Communications Functionality of Exchange Server 2007 : Installing OCS 2007 (part 1)
    Google, privacy & you (Part 1)
    iPhone Application Development : Making Multivalue Choices with Pickers - Understanding Pickers
    Microsoft Surface With Windows RT - Truly A Unique Tablet
    Network Configuration & Troubleshooting (Part 1)
    Panasonic Lumix GH3 – The Fastest Touchscreen-Camera (Part 2)
    Programming Microsoft SQL Server 2005 : FOR XML Commands (part 3) - OPENXML Enhancements in SQL Server 2005
    Exchange Server 2010 : Track Exchange Performance (part 2) - Test the Performance Limitations in a Lab
    Extra Network Hardware Round-Up (Part 2) - NAS Drives, Media Center Extenders & Games Consoles
    Windows Server 2003 : Planning a Host Name Resolution Strategy - Understanding Name Resolution Requirements
    Google’s Data Liberation Front (Part 2)
    Datacolor SpyderLensCal (Part 1)