是否可以在vsTemplates中添加条件?或基于用户选择生成输出项目的方法

问题描述 投票:0回答:1

我需要生成一个自定义项目模板。我向用户显示向导(使用vsix项目),该向导具有三个复选框,用户可以根据选择创建一个项目或多个项目。

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

目前,无论选择什么,都会创建三个输出项目。 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>
visual-studio vsix vs-extensibility visual-studio-templates vsixmanifest
1个回答
0
投票
这样,我们可以有条件地创建项目。

enter image description here

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

© www.soinside.com 2019 - 2024. All rights reserved.