为 Visual Studio 2008 创建一个具有文件依赖性的项目模板,是否可能?

发布于 2024-08-04 04:55:08 字数 2585 浏览 3 评论 0原文

我有一个模板,可将新条目添加到 Visual Studio 解决方案资源管理器中项目的“添加->新项目”右键菜单中。

我已经构建了模板,将其放入文档文件夹下的 ItemTemplates 目录中,并且它可以工作,因为我可以通过模板向项目添加新项目。

但是,该模板由 3 个文件组成:

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

这些文件被添加到同一级别的项目中,但我希望拥有与向项目添加表单时获得的相同类型的层次结构,其中 .Designer.cs 文件作为子节点放置在 .cs 文件下。

基本上,这就是该项目的样子:

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

而我希望它看起来像这样:

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

这可能吗?如果是这样,我应该在 .vstemplate 文件中更改什么才能获得此行为?

这是我添加到模板 zip 文件中的 .vstemplate 文件:

<VSTemplate Version="2.0.0" Type="Item" xmlns="http://schemas.microsoft.com/developer/vstemplate/2005">
    <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>

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">
    <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>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

鹿! 2024-08-11 04:55:08

在项目文件(例如 .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>

并将项目文件添加到您的 .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>

完成;-)

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 ;-)

浅语花开 2024-08-11 04:55:08

依赖文件需要引用“父”文件,如下所示:

    <ProjectItem ReplaceParameters="true">Controller.controller</ProjectItem>
    <ProjectItem ReplaceParameters="true" TargetFileName="Controller.controller\$fileinputname$.Designer.cs">Controller.Designer.cs</ProjectItem>
    <ProjectItem ReplaceParameters="true" TargetFileName="Controller.controller\$fileinputname$.cs">Controller.cs</ProjectItem>

The dependent files need to reference the "parent" file as such:

    <ProjectItem ReplaceParameters="true">Controller.controller</ProjectItem>
    <ProjectItem ReplaceParameters="true" TargetFileName="Controller.controller\$fileinputname$.Designer.cs">Controller.Designer.cs</ProjectItem>
    <ProjectItem ReplaceParameters="true" TargetFileName="Controller.controller\$fileinputname$.cs">Controller.cs</ProjectItem>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文