我開始將此作爲項目模板,但如果這不是合適的解決方案,請告訴我。當一個新的widget被添加到項目中時,我試圖自動創建幾個文件(以及可能的幾個文件夾)。我使用MVP模式,因此每個小部件都有5個類(IModel,Model,IView,View和Presenter),每個類都具有相同的基本名稱,但位於不同的文件夾中。文件夾結構類似於爲多個文件夾中的多個文件創建VS2013項目模板
+Models
+Interfaces
+ IModelOne
+ ModelOne
+ SubsetOne
+ Interfaces
+ IModelThree
+ ModelThree
+Views
+ Interfaces
+ IViewOne
+ SubsetOne
+ Interfaces
+ IViewThree
+ ViewThree
+Presenters
+ PresenterOne
+ SubsetOne
+ PresenterThree
模型,視圖和演示文稿文件夾具有相同的結構。我想要一種方法來添加模板中的新項目在同一個結構,基於1)他們點擊添加菜單項(例如,如果他們右擊演示者 - >子集一,並選擇添加新項目,它會以某種方式認識到SubsetOne是構建所有東西的「基本路徑」),或者2.)允許用戶通過嚮導類型的東西輸入該「基本路徑」。
如果不使用嚮導,我可以在模型,視圖和演示文件夾(或某些硬編碼的子目錄)頂層創建所有必需的文件。對於一些小部件(那些需要處於頂層的小部件),這是可以的,但是它的使用將非常有限。
我按照http://msdn.microsoft.com/en-us/library/ms185301.aspx的說明完成設置。嘗試使用嚮導時出現的問題是,它彈出一條消息,指出「值不在預期範圍內」。我嘗試通過http://social.msdn.microsoft.com/Forums/vstudio/en-US/005df602-5b22-4a33-b03c-df6cf9f97715/step-by-step-instruction-how-to-debug-a-custom-wizard-template-needed?forum=vsx上的步驟進行調試,但是斷點從未被擊中,它表示符號未加載。但是,我的嚮導類中的消息框永遠不會顯示,所以我懷疑該錯誤不在嚮導類中。如果我可以在我的機器上使用這個工具,我希望能夠將這個工作交給團隊中的其他開發人員使用。
我的嚮導類代碼:
/// <summary>
/// A wizard class to receive additional input from the
/// user before creating the set of MVP classes.
/// </summary>
public class MvpWizard : IWizard
{
/// <summary>
/// Runs custom wizard logic at the beginning of a template wizard run.
/// </summary>
/// <param name="automationObject">The automation object being used by the template
/// wizard.</param>
/// <param name="replacementsDictionary">The list of standard parameters to be replaced.</param>
/// <param name="runKind">A <see cref="T:Microsoft.VisualStudio.TemplateWizard.WizardRunKind"/> indicating
/// the type of wizard run.</param><param name="customParams">The custom parameters with which to
/// perform parameter replacement in the project.</param>
public void RunStarted(object automationObject, Dictionary<string, string> replacementsDictionary,
WizardRunKind runKind, object[] customParams)
{
MvpWizardForm form = new MvpWizardForm();
try
{
form.ShowDialog();
replacementsDictionary.Add("$BasePath$", form.BasePath);
replacementsDictionary.Add("$PresenterPath$", form.PresenterPath);
}
catch (Exception e)
{
MessageBox.Show("An exception occured: ", e.Message);
}
}
/// <summary>
/// Runs custom wizard logic when a project has finished generating.
/// </summary>
/// <param name="project">The project that finished generating.</param>
public void ProjectFinishedGenerating(Project project) {}
/// <summary>
/// Runs custom wizard logic when a project item has finished generating.
/// </summary>
/// <param name="projectItem">The project item that finished generating.</param>
public void ProjectItemFinishedGenerating(ProjectItem projectItem) {}
/// <summary>
/// Indicates whether the specified project item should be added to the project.
/// </summary>
/// <returns>
/// true if the project item should be added to the project; otherwise, false.
/// </returns>
/// <param name="filePath">The path to the project item.</param>
public bool ShouldAddProjectItem(string filePath)
{
return true;
}
/// <summary>
/// Runs custom wizard logic before opening an item in the template.
/// </summary>
/// <param name="projectItem">The project item that will be opened.</param>
public void BeforeOpeningFile(ProjectItem projectItem) {}
/// <summary>
/// Runs custom wizard logic when the wizard has completed all tasks.
/// </summary>
public void RunFinished() {}
而且.vstemplate代碼:
<VSTemplate Version="3.0.0" xmlns="http://schemas.microsoft.com/developer/vstemplate/2005" Type="Item">
<TemplateData>
<DefaultName>Mvp.cs</DefaultName>
<Name>MVP item</Name>
<Description>stuff</Description>
<ProjectType>CSharp</ProjectType>
<SortOrder>10</SortOrder>
<Icon>__TemplateIcon.png</Icon>
</TemplateData>
<TemplateContent>
<References />
<ProjectItem SubType="" TargetFileName="Models/Interfaces/I$fileinputname$Model.cs" ReplaceParameters="true">ITestModel.cs</ProjectItem>
<ProjectItem SubType="" TargetFileName="Models/$fileinputname$Model.cs" ReplaceParameters="true">TestModel.cs</ProjectItem>
<ProjectItem SubType="" TargetFileName="Views/Interfaces/I$fileinputname$View.cs" ReplaceParameters="true">ITestView.cs</ProjectItem>
<ProjectItem SubType="" TargetFileName="Views/$fileinputname$View.cs" ReplaceParameters="true">TestView.cs</ProjectItem>
<ProjectItem SubType="" TargetFileName="Views/$fileinputname$View.Designer.cs" ReplaceParameters="true">TestView.Designer.cs</ProjectItem>
<ProjectItem SubType="" TargetFileName="Presenters/$fileinputname$Presenter.cs" ReplaceParameters="true">TestPresenter.cs</ProjectItem>
<CustomParameters>
<CustomParameter Name="$Model$" Value="$fileinputname$Model"/>
<CustomParameter Name="$ModelInterface$" Value="I$fileinputname$Model"/>
<CustomParameter Name="$Presenter$" Value="$fileinputname$Presenter"/>
<CustomParameter Name="$View$" Value="$fileinputname$View"/>
<CustomParameter Name="$ViewInterface$" Value="I$fileinputname$View"/>
</CustomParameters>
</TemplateContent>
<WizardExtension>
<Assembly>MvpWizard, Version=1.0.0.0, Culture=Neutral, PublicKeyToken=37d8d03713727225</Assembly>
<FullClassName>MvpWizard.MvpWizard</FullClassName>
</WizardExtension>
</VSTemplate>
任何想法? TIA!
哦,如果這可行的話,那將是非常棒的。我面臨同樣的問題(爲單個功能創建大量文件),並且每次都會做同樣的啞巴工作。 – SeriousM