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,这使其成为编写自定义代码以收集用户输入的一个良好位置。
自动化与扩展性参考。
RunStarted 方法显示一个 Windows 窗体,该窗体允许用户添加一个自定义参数值,随后将在创建项目的过程中使用此值。
|
|
|---|
|
IWizard,但您也可以使用 Visual Basic。 |
实现 IWizard
-
创建一个新类库项目。
-
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 窗体
-
“添加新项”。
-
“确定”。
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(); } } }
在本示例中,用作模板的项目是一个控制台应用程序,它显示在自定义向导的用户输入窗体中指定的消息。
创建示例项目
-
创建一个新的 Visual C# 控制台应用程序。
-
Main 方法中,添加以下代码行。
Console.WriteLine("$custommessage$");$custommessage$ 将替换为在用户输入窗体中输入的文本。
-
“导出模板”。
-
“下一步”。
-
“完成”。
“新建项目”对话框中,但没有使用自定义向导。
下面的示例显示导出到模板之前的完整代码文件。
using System; using System.Collections.Generic; using System.Text; namespace TemplateProject { class WriteMessage { static void Main(string[] args) { Console.WriteLine("$custommessage$"); } } }
“新建项目”对话框中,必须对其进行修改,以便它使用在前面步骤中创建的程序集。
向模板添加自定义向导
-
找到包含该模板的 .zip 文件。
-
“选项”。
-
“项目和解决方案”。
-
“选项”对话框 ->“项目和解决方案”->“常规”。
默认情况下,此位置为 My Documents\Visual Studio 2010\Templates\ProjectTemplates。
-
-
解压缩该 .zip 文件。
-
在 Visual Studio 中打开 .vstemplate 文件。
-
如何:引用具有强名称的程序集。
下面的示例显示一个 WizardExtension 元素。
<WizardExtension> <Assembly>CustomWizard, Version=1.0.0.0, Culture=Neutral, PublicKeyToken=fa3902f409bb6a3b</Assembly> <FullClassName>CustomWizard.IWizardImplementation</FullClassName> </WizardExtension>