简体   繁体   中英

How to add an item based on a specific template when click on menu Add item?

I want to create a template for example (Create a folder in the folder create 1 class and 1 interface with the names of the folder) and it will be created by right click on the project and in the menu, it will appear CreateTamplateClass

Can I do it using c# WinForms / Console Application and more .. ?

TNX

Visual Studio multi-file Item Template having Folder with parameter replacement

You can create a C# Item Template project or manually create the item template zip file yourself.

The item template zip file should contain the following files:

  • Class.cs
  • Interface.cs
  • MyItemTemplate.vstemplate

Then you need to copy the zip file to following folder:

  • %USERPROFILE%\\Documents\\Visual Studio 2017\\Templates\\ItemTemplates\\Visual C#

Then open a new instance of Visual Studio and create/open a C# project, then open Add New Item window and choose MyItemTemplate from Visual C# Items and assing a name like MyItem1 , then the following structure will be created:

具有文件夹的 Visual Studio 多文件项模板

Here is the content for template files in the zip file:

Class.cs

using System;
using System.Collections.Generic;

namespace $rootnamespace$.$basename$
{
    class $safeitemrootname$: I$safeitemrootname$
    {
    }
}

Interface.cs

using System;
using System.Collections.Generic;

namespace $rootnamespace$.$basename$
{
    interface $safeitemrootname$
    {
    }
}

MyItemTemplate.vstemplate

<?xml version="1.0" encoding="utf-8"?>
<VSTemplate Version="3.0.0" Type="Item" xmlns="http://schemas.microsoft.com/developer/vstemplate/2005" xmlns:sdk="http://schemas.microsoft.com/developer/vstemplate-sdkextension/2010">
  <TemplateData>
    <Name>MyItemTemplate</Name>
    <Description>My multi-file item template</Description>
    <Icon>MyItemTemplate.ico</Icon>
    <TemplateID>52ae3bdb-7fde-4d47-8a4b-d17d0c9269f7</TemplateID>
    <ProjectType>CSharp</ProjectType>
    <DefaultName>MyItem.cs</DefaultName>
  </TemplateData>
  <TemplateContent>
    <References>
      <Reference>
        <Assembly>System</Assembly>
      </Reference>
    </References>
    <ProjectItem TargetFileName="$fileinputname$\I$fileinputname$.cs" ReplaceParameters="true">Interface.cs</ProjectItem>
    <ProjectItem TargetFileName="$fileinputname$\$fileinputname$.cs" ReplaceParameters="true">Class.cs</ProjectItem>
    <CustomParameters>
      <CustomParameter Name="$basename$" Value="$fileinputname$"/>
    </CustomParameters>    
  </TemplateContent>
</VSTemplate>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM