有没有办法在 vsTemplates 中添加条件?或根据用户选择生成输出项目的方法

Is there a way to add conditions in vsTemplates? or a way to generate output projects based on user selections

我需要生成自定义项目模板。我正在向用户展示向导(使用 vsix 项目),该向导具有三个复选框,用户可以在其中 select 一个或多个复选框,具体取决于 selection 需要创建的项目数量.

假设用户选择一个,那么输出的解决方案必须有一个项目。 假设用户选择任意两个,那么输出解决方案必须有这两个项目。

目前,无论 selection 是否创建了三个输出项目。根 vsTemplate 文件如下所示,有没有办法实现我的要求?

<ProjectCollection>
      <ProjectTemplateLink ProjectName="Cat">
        CatProject\Cat.vstemplate
      </ProjectTemplateLink>
      <ProjectTemplateLink ProjectName="Dog">
        DogProject\Dog.vstemplate
      </ProjectTemplateLink>
      <ProjectTemplateLink ProjectName="Lion">
        LionProject\Lion.vstemplate
      </ProjectTemplateLink>
</ProjectCollection>

我尝试了两种方法 1) 在 运行 时间相应地更改根 vsTemplate 文件,但始终基于 bin 目录中的 zip 文件夹中的 vsTemplate 文件创建输出项目。 2)使用开发工具环境自动化对象,在 IWizard 接口的 ProjectItemFinished 方法中以编程方式添加或删除项目,示例: Dte.Solution.AddFromTemplate(vsTemplatePath, NewProjectName) 上面一行创建了一个新项目,但是缺少文件夹结构,很难调整它。

有没有简单的方法可以做到这一点?

<ProjectCollection>
      if(cat is selected)
      <ProjectTemplateLink ProjectName="Cat">
        CatProject\Cat.vstemplate
       </ProjectTemplateLink>
        end if
      if(Dog is selected)
      <ProjectTemplateLink ProjectName="Dog">
        DogProject\Dog.vstemplate
      </ProjectTemplateLink>
      end if
      if(Lion is selected)
      <ProjectTemplateLink ProjectName="Lion">
        LionProject\Lion.vstemplate
      </ProjectTemplateLink>
      end if
</ProjectCollection>

我使用我尝试过的第二种方法让它工作。这是完整的解决方案。记住我们在项目中添加新的vstemplate文件时build action type必须是"vstemplate",告诉soln.GetProjectTemplate去寻找vstemplate类型的文件,否则可能会报错。

这样我们就可以有条件的创建项目了。

 using EnvDTE;
  using EnvDTE80;  

  DTE dte;

public void ProjectFinishedGenerating(Project project)
        {
            //Accessing DTE(Development tool environment) object should only be done on main thread, otherwise throw exception
            ThreadHelper.ThrowIfNotOnUIThread();
            Solution2 soln = (Solution2)dte.Solution;
            if (firstForm.IsCatSelected)
            {                
                dte.Solution.AddFromTemplate(soln.GetProjectTemplate("Cat.vstemplate", "CSharp"), destinationDirectory + "\Cat", "Cat");               
            }
            if (firstForm.IsDogSelected)
            {
                dte.Solution.AddFromTemplate(soln.GetProjectTemplate("Dog.vstemplate", "CSharp"), destinationDirectory + "\Dog", "Dog");
            }
        }

public void RunStarted(object automationObject,
            Dictionary<string, string> replacementsDictionary,
            WizardRunKind runKind, object[] customParams)
        {
            //Accessing DTE(Development tool environment) object should only be done on main thread, otherwise throw exception
            ThreadHelper.ThrowIfNotOnUIThread();
            destinationDirectory = replacementsDictionary["$destinationdirectory$"];

            dte = automationObject as DTE;
          }