Visual Studio 提供了 IWizard 接口,在实现该接口后,您可以在用户根据模板创建项目时运行自定义代码。

http://msdn.microsoft.com/zh-cn/library/ms185301(v=vs.100).aspx

 http://msdn.microsoft.com/zh-cn/library/ms247119(v=vs.100).aspx

IWizard 接口,在实现该接口后,您可以在用户根据模板创建项目时运行自定义代码。

项目模板的自定义可用于:

  • 显示收集用户输入以参数化模板的自定义 UI。

  • 添加要在模板中使用的参数值。

  • 向模板添加其他文件。

  • 执行项目的 Visual Studio 自动化对象模型允许的几乎任何操作。

RunStarted,这使其成为编写自定义代码以收集用户输入的一个良好位置。

自动化与扩展性参考

主要步骤如下所示,其中每一步都有详细解释。

创建自定义模板向导

  1. IWizard 接口的程序集。

  2. 将此程序集安装到全局程序集缓存中。

  3. “导出模板”向导根据该项目创建模板。

  4. IWizard 的程序集。

  5. 使用自定义向导创建新项目。

RunStarted 方法显示一个 Windows 窗体,该窗体允许用户添加一个自定义参数值,随后将在创建项目的过程中使用此值。

注意

IWizard,但您也可以使用 Visual Basic。

实现 IWizard

  1. 创建一个新类库项目。

  2. IWizard 接口。

UserInputForm,它是用于获得用户输入的 Windows 窗体。

IWizardImplementation 类

true

RunStarted 方法接受四个参数:

  • _DTE 对象,以使您能够自定义项目。

  • 模板参数

  • WizardRunKind 参数,它包含有关所使用的模板种类的信息。

  • Object 数组,它包含通过 Visual Studio 传递给向导的一组参数。

$custommessage$ 参数的每个实例都将替换为用户输入的文本。

 
using System;
using System.Collections.Generic;
using Microsoft.VisualStudio.TemplateWizard;
using System.Windows.Forms;
using EnvDTE;

namespace CustomWizard
{
    public class IWizardImplementation:IWizard
    {
        private UserInputForm inputForm;
        private string customMessage;

        // 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 
                // input for the custom message.
                inputForm = new UserInputForm();
                inputForm.ShowDialog();

                customMessage = inputForm.get_CustomMessage();

                // Add custom parameters.
                replacementsDictionary.Add("$custommessage$", 
                    customMessage);
            }
            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;
        }        
    }
}

用户输入窗体

customMessage 参数中。

向解决方案添加 Windows 窗体

  1. “添加新项”

  2. “确定”

 
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace CustomWizard
{
    public partial class UserInputForm : Form
    {
        private string customMessage;

        public UserInputForm()
        {
            InitializeComponent();
        }
       
        public string get_CustomMessage()
        {
            return customMessage;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            customMessage = textBox1.Text;

            this.Dispose();
        }
    }
}

IWizard 的程序集进行签名,并将该程序集安装到全局程序集缓存中。

将程序集安装到全局程序集缓存中

  1. 如何:对程序集进行签名 (Visual Studio)

  2. 如何:将程序集安装到全局程序集缓存

在本示例中,用作模板的项目是一个控制台应用程序,它显示在自定义向导的用户输入窗体中指定的消息。

创建示例项目

  1. 创建一个新的 Visual C# 控制台应用程序。

  2. Main 方法中,添加以下代码行。

     
     
    Console.WriteLine("$custommessage$");
    

    $custommessage$ 将替换为在用户输入窗体中输入的文本。

  3. “导出模板”

  4. “下一步”

  5. “完成”

    “新建项目”对话框中,但没有使用自定义向导。

下面的示例显示导出到模板之前的完整代码文件。

 
using System;
using System.Collections.Generic;
using System.Text;

namespace TemplateProject
{
    class WriteMessage
    {
        static void Main(string[] args)
        {
            Console.WriteLine("$custommessage$");
        }
    }
}

“新建项目”对话框中,必须对其进行修改,以便它使用在前面步骤中创建的程序集。

向模板添加自定义向导

  1. 找到包含该模板的 .zip 文件。

    1. “选项”

    2. “项目和解决方案”

    3. “选项”对话框 ->“项目和解决方案”->“常规”

    默认情况下,此位置为 My Documents\Visual Studio 2010\Templates\ProjectTemplates。

  2. 解压缩该 .zip 文件。

  3. 在 Visual Studio 中打开 .vstemplate 文件。

  4. 如何:引用具有强名称的程序集

    下面的示例显示一个 WizardExtension 元素。

     
     
    <WizardExtension>
        <Assembly>CustomWizard, Version=1.0.0.0, Culture=Neutral, PublicKeyToken=fa3902f409bb6a3b</Assembly>
        <FullClassName>CustomWizard.IWizardImplementation</FullClassName>
    </WizardExtension>
    

现在,您可以根据自己的模板创建项目并使用自定义向导。

使用自定义向导

  1. “新建项目”

  2. “确定”

    向导用户输入窗体将打开。

  3. 为自定义参数键入一个值并单击按钮。

    向导用户输入窗体将关闭,并且根据模板创建了一个项目。

  4. “查看代码”

    $custommessage$ 已替换为在向导用户输入窗体中输入的文本。

相关文章: