多个项目中的多文件项模板

Ecc*_*kit 5 templates visual-studio-2013

我可以制作一个项目模板,将多个文件插入到同一解决方案下的不同项目中吗?

我不打算在同一个项目中的不同文件夹下添加文件.

这就是我要找的东西:

  • 项目1

    • 模板项目1
  • 项目2

    • 模板项目2

小智 1

好的,基于 VS 2017,我创建了一个 VSIX 项目、一个库项目和一个项目模板项目

1-在项目模板项目中,我创建项目模板文件一和二。我还在 vstemplate 文件中添加了 WizardExtension 信息,如下所示

    <TemplateContent>
    <ProjectItem ReplaceParameters="true" TargetFileName="$fileinputname$Service.cs">Service.cs</ProjectItem>
    <ProjectItem ReplaceParameters="true" TargetFileName="$fileinputname$DTO.cs">DTO.cs</ProjectItem>
    <ProjectItem ReplaceParameters="true" TargetFileName="$fileinputname$Message.cs">Message.cs</ProjectItem>
  </TemplateContent>
  <WizardExtension>
    <Assembly>WizardImplementation, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null</Assembly>
    <FullClassName>WizardImplementation.WizardImplementation</FullClassName>
  </WizardExtension>
Run Code Online (Sandbox Code Playgroud)

WizardImplementation 是我的类库项目。

2-我将 TemplateWizardInterface Nuget 包添加到类库项目中,并在类中实现 IWizard。在 ProjectItemFinishedGenerating 中,我将第二个模板项(如文件)移动到第二个项目位置文件夹,并使用 Microsoft.Build dll 中的 Microsoft.Build.Evaluation.ProjectCollection 以编程方式将其添加到项目中。

public void ProjectItemFinishedGenerating(ProjectItem projectItem)
        {
            if (projectItem.FileNames[0].Contains("DTO") || projectItem.FileNames[0].Contains("Message"))
            {
                string newPath = Path.GetFullPath(Path.Combine(projectItem.FileNames[0], @"..\Project2\"));
                if (!Directory.Exists(newPath))
                {
                    MessageBox.Show($"Message path does not exist. \r\n {newPath}");
                }
                else
                {
                    var newFullPath = Path.Combine(newPath, projectItem.Name);
                    File.Move(projectItem.FileNames[0], newFullPath);
                    var p = Microsoft.Build.Evaluation.ProjectCollection.GlobalProjectCollection.GetLoadedProjects(PathInformation.MessageProjectPath).FirstOrDefault();
                    if (p == null)
                        p = new Microsoft.Build.Evaluation.Project(PathInformation.MessageProjectPath);

                    var res = p.AddItem("Compile", newFullPath);
                    p.Save();
                    if(res == null)
                    {
                        MessageBox.Show("Nothing added to project");
                    }
                    else
                    {
                        MessageBox.Show($"{res.Count()} item added to project");
                    }
                }
            }
        }
Run Code Online (Sandbox Code Playgroud)

有关 IWizard 的更多信息,请查看此链接 https://msdn.microsoft.com/en-us/library/ms185301.aspx

3-在 VSIX 项目中,我将在第二步中创建的类库按程序集类型添加到 vsixmanifest 中的资产中。另外,我将项目模板项目添加为文件,并在 bin 中构建项目项目后提供 VS 创建的 zip 文件。