到了第(2)篇後,已經可以新增多個專案。缺點是,使用 Project template 時,無法指定專案的名稱,namespace 等問題。最後一篇就來說明如何解決。
(1)新增一個類別庫 (Class library) 並命名為CCWebSolutionTemplateWizard。
(2)在類別庫中新增一個Windows Form 並命名為 VariablesForm
(3)修改VariablesForm,拖曳成如下的畫面
(4)修改 VariablesForm.cs 如下方的程式
using System;
using System.Windows.Forms;
namespace CCWebSolutionTemplateWizard
{
public partial class VariablesForm : Form
{
public VariablesForm()
{
InitializeComponent();
}
private void btnOk_Click(object sender, EventArgs e)
{
this.Hide();
}
public string NameSpace
{
get { return txtNamespace.Text; }
}
public string WebSiteName
{
get { return txtWebSiteName.Text; }
}
public string BusinessComponentsName
{
get { return txtBusinessComponents.Text; }
}
}
}
(5) 新增參考 Microsoft.VisualStudio.TemplateWizardInterface, EnvDTE
(6) 新增一個 class 並命名為 CCWizard.cs ,內容如下
using System;
using System.Collections.Generic;
namespace CCWebSolutionTemplateWizard
{
class CCWizard : Microsoft.VisualStudio.TemplateWizard.IWizard
{
public void BeforeOpeningFile(EnvDTE.ProjectItem projectItem) { }
public void ProjectFinishedGenerating(EnvDTE.Project project) { }
public void ProjectItemFinishedGenerating(EnvDTE.ProjectItem projectItem) { }
public void RunFinished() { }
public void RunStarted(object automationObject, Dictionary<string, string> replacementsDictionary, Microsoft.VisualStudio.TemplateWizard.WizardRunKind runKind, object[] customParams)
{
var form = new VariablesForm();
try
{
form.ShowDialog();
replacementsDictionary.Add("$NameSpace$", form.NameSpace);
replacementsDictionary.Add("$WebSiteName$", form.WebSiteName);
replacementsDictionary.Add("$BusinessComponentsName$", form.BusinessComponentsName);
}
catch (Exception ex)
{
System.Windows.Forms.MessageBox.Show(ex.ToString());
}
}
public bool ShouldAddProjectItem(string filePath) { return true; }
}
}
(7) 為了在新增專案時能使用自訂的Wizard,必須讓CCWebSolutionTemplateWizard類別庫註冊到 GAC中,因此必須有 strong name。 請到CCWebSolutionTemplateWizard的「property」內Siging頁設一個 key吧,如下圖。再 build 一次,讓CCWebSolutionTemplateWizard.dll 具有 strong name
(8)將CCWebSolutionTemplateWizard.dll 註冊到 GAC中。簡單的方法是使用檔案總管將 dll 拖曳到 c:\windows\assembly 中。或者使用下面語法
gacutil /i CCWebSolutionTemplateWizard.dll
(9) 在上一篇中,Solution folder 中的 MyTemplate.vstemplate 中,WebSiteName 是 hard code 的。這一次就必須在使用自訂的 project template 時,使用我們自訂的 wizard,指定 Web Site Name。於是,我們修改 MyTemplate.vstemplate 如下
<?xml version="1.0"?>
<VSTemplate xmlns="http://schemas.microsoft.com/developer/vstemplate/2005" Version="2.0.0" Type="ProjectGroup">
<TemplateData>
<Name>CCWebWebSolution</Name>
<Description>CC web solution starter template</Description>
<Icon>__TemplateIcon.ico</Icon>
<ProjectType>CSharp</ProjectType>
<ProjectSubType>Web</ProjectSubType>
</TemplateData>
<TemplateContent>
<ProjectCollection>
<ProjectTemplateLink ProjectName="$WebSiteName$">CCWebApplication\MyTemplate.vstemplate</ProjectTemplateLink>
<ProjectTemplateLink ProjectName="$BusinessComponentsName$">BusinessComponents\MyTemplate.vstemplate</ProjectTemplateLink>
</ProjectCollection>
</TemplateContent>
<WizardExtension>
<Assembly>CCWebSolutionTemplateWizard, Version=1.0.0.0, Culture=neutral, PublicKeyToken=182050d61235b264, processorArchitecture=MSIL</Assembly>
<FullClassName>CCWebSolutionTemplateWizard.CCWizard</FullClassName>
</WizardExtension>
</VSTemplate>
其中 <Assmebly>…</Assembly>的值,可由 gacutil /l CCWebSolutionTemplateWizard 指令取得。
(10) 再由上一篇中介紹的,將檔案壓縮成 zip 檔並放到 ProjectTemplate中。
(11) 大功告成了!新增專案時,就會使用我們的 wizard 了。可替的文字,可以是在 vstemplate檔中,也可以是 cs 的原始程式碼。最後的範例可
下載