views:

366

answers:

1

I have a template that adds a new entry to the "Add->New item" right-click menu on a project in the solution explorer in Visual Studio.

I have already built the template, put it into my ItemTemplates directory beneath my documents folder, and it works, in the sense that I can add new items to the project through the template.

However, the template consists of 3 files:

<filename>.controller
<filename>.Designer.cs
<filename>.cs

These are added to the project at the same level, but I'd like to have the same kind of hierarchy you get when you add a form to the project, where the .Designer.cs file is placed as a sub-node beneath the .cs file.

Basically, this is what the project looks like:

TestProject
  +- Properties
  +- References
  +- App.config
  +- Program.cs
  +- MyTestController.controller
  +- MyTestController.Designer.cs
  +- MyTestController.cs

whereas I want it to look like this:

TestProject
  +- Properties
  +- References
  +- App.config
  +- Program.cs
  +- MyTestController.controller
     +- MyTestController.Designer.cs
     +- MyTestController.cs

is this possible? If so, what do I change in my .vstemplate file to get this behaviour?

Here's the .vstemplate file I've added to the template zip file:

<VSTemplate Version="2.0.0" Type="Item" xmlns="http://schemas.microsoft.com/developer/vstemplate/2005"&gt;
    <TemplateData>
        <Name>LVK.NET New Controller</Name>
        <Description>Adds an business logic controller class to the project.</Description>
        <Icon Package="{FAE04EC1-301F-11d3-BF4B-00C04F79EFBC}" ID="4522" />
        <ProjectType>CSharp</ProjectType>
        <SortOrder>10</SortOrder>
        <DefaultName>Controller.controller</DefaultName>
    </TemplateData>
    <TemplateContent>
        <References>
            <Reference>
                <Assembly>System</Assembly>
            </Reference>
            <Reference>
                <Assembly>System.Data</Assembly>
            </Reference>
            <Reference>
                <Assembly>System.Xml</Assembly>:\
            </Reference>
            <Reference>
                <Assembly>LVK.Core</Assembly>
            </Reference>
            <Reference>
                <Assembly>LVK.BusinessLogic</Assembly>
            </Reference>
        </References>
        <ProjectItem ReplaceParameters="true">Controller.controller</ProjectItem>
        <ProjectItem ReplaceParameters="true" TargetFileName="$fileinputname$.Designer.cs">Controller.Designer.cs</ProjectItem>
        <ProjectItem ReplaceParameters="true" TargetFileName="$fileinputname$.cs">Controller.cs</ProjectItem>
    </TemplateContent>
</VSTemplate>
+1  A: 

Specify your "hierachy" in the projectfile (e.g. .csproj):

<ItemGroup>
  <Compile Include="Controller.controller" />
  <Compile Include="Controller.cs">
    <DependentUpon>Controller.controller</DependentUpon>
  </Compile>
  <Compile Include="Controller.Designer.cs">
    <DependentUpon>Controller.controller</DependentUpon>
  </Compile>
</ItemGroup>

and add the project file to your .vstemplate

  <TemplateContent>
    <Project TargetFileName="Project1.csproj" File="Project1.csproj" ReplaceParameters="true">
      <ProjectItem ReplaceParameters="false" TargetFileName="Controller.controller">Controller.controller</ProjectItem>
      <ProjectItem ReplaceParameters="true" TargetFileName="Controller.cs">Controller.cs</ProjectItem>
      <ProjectItem ReplaceParameters="true" TargetFileName="Controller.Designer.cs">Controller.Designer.cs</ProjectItem>
    </Project>
  </TemplateContent>

Done ;-)

Sven