The first step in the process is to create an assembly that implements IWizard. This assembly uses the RunStarted(Object, Dictionary<(Of <(String, String>)>), WizardRunKind, array<Object>[]()[]) method to display a Windows Form that allows the user to add a custom parameter value, which will then be used during project creation.
Note: |
|---|
This example uses
Visual C# to implement IWizard, although you could also use Visual J# or Visual Basic.
|
To implement IWizard
Create a new class library project.
Create a class that implements the IWizard interface. See the code below for a Visual C# example of a fully implemented IWizard interface.
This example contains two code files: IWizardImplementation, a class that implements the IWizard interface, and UserInputForm, the Windows Form for user input.
IWizardImplementation Class
The IWizardImplementation class contains method implementations for every member of IWizard. In this example, only the RunStarted(Object, Dictionary<(Of <(String, String>)>), WizardRunKind, array<Object>[]()[]) method performs a task. All other methods either do nothing or return true.
The RunStarted(Object, Dictionary<(Of <(String, String>)>), WizardRunKind, array<Object>[]()[]) method accepts four parameters:
An Object parameter that can be cast to the root _DTE object, to enable you to customize the project.
A Dictionary parameter that contains a collection of all pre-defined parameters in the template. For more information on template parameters, see Template Parameters.
A WizardRunKind parameter that contains information about what kind of template is being used.
An Object array that contains a set of parameters passed to the wizard by Visual Studio.
This example adds a parameter value from the user input form to the Dictionary parameter. Every instance of the $custommessage$ parameter in the project will be replaced with the text entered by the user.
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;
}
}
}
User Input Form
The user input form provides a simple form for entering a custom parameter. The form contains a text box named textBox1 and a button named button1. When the button is clicked, the text from the text box is stored in the customMessage parameter.
To add a Windows Form to the solution
On the Project menu, click Add New Item.
Click Windows Form, name the file UserInputForm.cs, and click OK.
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();
}
}
}