首页 > 解决方案 > 如何将用户在向导中检查的类添加到项目模板?

问题描述

我正在为 Visual Studio 开发一个扩展,目标是允许用户通过 TreeView 选择类,并在单击 Ok 按钮后使用选定的类生成项目。这些类通过反射从特定文件夹中的程序集加载。所以我可以加载程序集并通过 TreeView 用户可以选择他想要的类。重点是如何将这些类放在一个集合中,并在生成的项目模板中创建文件夹结构和类。有人对如何做到这一点有建议吗?

我的解决方案有 2 个项目:向导和模板:

我的解决方案: 在此处输入图像描述

树视图: 树视图

我的向导实现:

using EnvDTE;
using Microsoft.VisualStudio.TemplateWizard;
using Primavera.Extensibility.Wizard.UI;
using System;
using System.Collections.Generic;
using System.Windows.Forms;

namespace Primavera.Extensibility.Wizard
{
    public class WizardImplementation : IWizard
    {
        private Modules frm;

        // This method is called before opening any item that   
        // has the OpenInEditor attribute.  
        public void BeforeOpeningFile(ProjectItem projectItem)
        {
        }

        public void ProjectFinishedGenerating(Project project)
        {
        }

        // This method is only called for item templates,  
        // not for project templates.  
        public void ProjectItemFinishedGenerating(ProjectItem
            projectItem)
        {
        }

        // This method is called after the project is created.  
        public void RunFinished()
        {
        }

        public void RunStarted(object automationObject,
            Dictionary<string, string> replacementsDictionary,
            WizardRunKind runKind, object[] customParams)
        {
            try
            {
                // Display a form to the user. The form collects the classes checked 
                frm = new Modules();
                frm.ShowDialog();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }
        }

        // This method is only called for item templates,  
        // not for project templates.  
        public bool ShouldAddProjectItem(string filePath)
        {
            return true;
        }
    }
}

标签: c#.netvisual-studiovsixproject-template

解决方案


推荐阅读