次の方法で共有


Roll Your Own Item Template with the Export Template Wizard

Menu:  File -> Export Template
Command:  File.ExportTemplate
Versions:  2008,2010
Published:  8/17/2010
Code:  vstipProj0013

 

In vstipProj0004 I showed you how to roll your own project template.  Now we are going to look at how to make your own item template.  Recall that a project template encompasses an entire project and all the files in it.  Sometimes we just want to customize an individual item that we use frequently in projects. 

 

Let's do an example.  Make a new project and then add a class to it (CTRL + SHIFT + A):

 

NOTE:  The process is the same regardless of language used.

image

 

 

Here is the way the code looks before modification:

image

 

 

Now modify it the way you would like your item template to look and save your changes to the file:

image

 

 

Now it's time to export our item template so we can use it in future projects.  Go to File -> Export Template:

 

NOTE:  It may will prompt you to save changes to your project if you haven't already.

image

 

 

Choose "Item Template" and select the correct project where the item you want is at (if there is more than one in your solution):

image

 

 

Click "Next".  Select the item to export as a template:

 

NOTE:  The wizard will automatically select any dependent files as needed.

image

 

 

Click "Next".  Now you can select any references you want included with the item.  Normally, you would use an "Imports" or "Using" statement as appropriate in your code rather than need to use this dialog as we did in our example; however this is a chance to add additional references as needed:

 

NOTE:  The list of assemblies is generated from the assembly references in the current project. If the assembly you want to reference does not appear in the list, exit the wizard and add the reference to your project, and then run the wizard again.

image

 

 

Click "Next".  As you can see there is quite a bit here to put in:

image

 

Template name-this is a friendly name you want the template to be called when you pick it from the list of templates.

 

Template description-short description that gives a little more detail on the template's purpose.

 

Icon Image-small image that represents the icon for the item.

 

Preview Image-a larger image that gives a preview of what the template looks like.

 

Output location-the location where exported items will go.

 

Automatically import the template into Visual Studio-let's you decide if you want the template imported right away or want to do it manually later.

 

Display an explorer window on the output files folder-opens up the location where the template files are stored once they are created.

 

Here are the settings I'm using for this example:

image

 

Click "Finish".  It will open up the output file location and show the .zip file that contains the templates:

image

 

Looking inside the file we see this:

image

 

Finally, we can create a new item (CTRL + SHIFT + A) and see our new template:

image

 

The template can also be seen at "..\Users\ username\Documents\Visual Studio <version>\Templates\ItemTemplates\" folder:

image

 

If you aren't happy with your new template just delete the .zip file from this directory and it will no longer show up in the Add New Item dialog.

Comments