有没有一种方法可以在vsTemplates中添加条件?或基于用户选择生成输出项目的方法 [英] Is there a way to add conditions in vsTemplates? or a way to generate output projects based on user selections

查看:104
本文介绍了有没有一种方法可以在vsTemplates中添加条件?或基于用户选择生成输出项目的方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have a requirement to generate a custom projects template. I am showing the wizard (using vsix project) to the user which has three check boxes in which user can select one or many of them, based on the selection the number of projects need to be created.

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

Suppose user choose one, then output solution must have one project. Suppose user choose any two, then output solution must have those two projects.

目前,无论选择什么,都会创建三个输出项目. vsTemplate根文件如下所示,有没有办法满足我的要求?

At Present irrespective of selection three output projects are created. The root vsTemplate file looks like below, Is there a way achieve my requirement?

<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) 上一行创建了一个新项目,但是缺少文件夹结构,并且很难对其进行调整.

I tried two ways of doing it 1)At run time changing the root vsTemplate file accordingly , but always output project is created based on the vsTemplate file which is already in zip folder in bin directory. 2)using Development tool environment automation object , adding or removing projects programatically in ProjectItemFinished method of IWizard interface ,Example: Dte.Solution.AddFromTemplate( vsTemplatePath, NewProjectName) The above line creates a new project but folder structure is missed, and it is difficult to tweak it.

有什么简单的方法可以做到这一点吗?

Is there any easy way to do like this?

<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文件时,构建操作类型必须为"vstemplate",以告知soln.GetProjectTemplate查找文件的vstemplate类型,否则可能会引发错误.

I made it work using the second approach I tried. Here is the complete solution. Remember when we add new vstemplate file to the project build action type must be "vstemplate" , to tell soln.GetProjectTemplate to look for vstemplate type of file, otherwise it may throw an error.

通过这种方式,我们可以有条件地创建项目.

In this way we can conditionally create projects.

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

这篇关于有没有一种方法可以在vsTemplates中添加条件?或基于用户选择生成输出项目的方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆