代码之家  ›  专栏  ›  技术社区  ›  Rockdocta

如何选择性地应用VSTemplate?

  •  3
  • Rockdocta  · 技术社区  · 13 年前

    我正在为我的公司创建一个用于MVC 4应用程序的自定义VSTemplate,它使用的向导与创建新MVC4应用程序时出现的向导相当。当开发人员创建这种类型的新应用程序时,我想应用两个模板之一,如下所示:

    VS Project Wizard

    这两个条目都对应于在我的VSIX项目中名为ProjectTemplates的文件夹下定义的模板:

    Solution Explorer

    我的问题是,当向导运行时,如何应用正确的模板?我知道如何创建一个包含多个项目的vstemplate(使用vstemplate中的ProjectCollection节点),但这不是我真正想做的,因为它们永远不会一起部署。我看到我可以将两个vstemplates作为Assets添加到我的vsixmanifest文件中,但我真的不确定如何有条件地只应用一个模板。

    谢谢

    1 回复  |  直到 13 年前
        1
  •  7
  •   Troy    12 年前

    您需要将“可选”模板的文件包含在“根”模板文件夹的子目录中,但 排除 他们来自 TemplateContent Element “根”模板的。

    你的 IWizard 实现需要保留对 EnvDTE.DTE 对象(的第一个参数 RunStarted )并在 ProjectFinishedGenerating 使用与用户选择的模板相匹配的模板将项目添加到解决方案中。

    public class SelectTemplatesWizard : IWizard
    {
        private EnvDTE.DTE _dte = null;
    
        private string _solutionDir = null;
    
        private string _templateDir = null;
    
        public void RunStarted(object automationObject, Dictionary<string, string> replacementsDictionary, WizardRunKind runKind, object[] customParams)
        {
            // Store the reference to the environment for later use
            _dte = automationObject as EnvDTE.DTE;
    
            /*
              The value of the item in the replacements dictionary for the key 
              $destinationdirectory$ is populated with the physical location of
              the directory (named based on the user entered project name) created
              sibling to the solution file.
    
              The solution directory will be this directories parent 
              when the Type attribute of the VSTemplate element is ProjectGroup
            */
            _solutionDir = System.IO.Path.GetDirectoryName(replacementsDictionary["$destinationdirectory$"]);
    
            // customParams[0] is a default custom param that contains the physical location of the template that is currently being applied
            _templateDir = System.IO.Path.GetDirectoryName(customParams[0] as string);
        }
    
        public void ProjectFinishedGenerating(Project project)
        {
            int userSelected = 1;
            string name= null, projectPath= null, templatePath = null;
    
            switch (userSelected)
            {
                case 0:
                    {
                        name = "Angular";
                        projectPath = System.IO.Path.Combine(_solutionDir, "Angular");
                        templatePath = System.IO.Path.Combine(_templateDir , "Angular\Angular.vstemplate");
                    }
                    break;
                case 1:
                    {
                        name = "MVC4";
                        projectPath = System.IO.Path.Combine(_solutionDir, "MVC4");
                        templatePath = System.IO.Path.Combine(_templateDir , "MVC4\MVC4.vstemplate");
                    }
                    break;
            }
            _dte.Solution.AddFromTemplate(templatePath, projectPath, name);
        }
    
        /* Other IWizard methods excluded for brevity */
    }