我将其作为项目模板启动,但如果这不是合适的解决方案,请告诉我。当一个新的小部件添加到项目中时,我试图自动创建几个文件(以及可能的几个文件夹)。我使用的是MVP模式,因此每个小部件都有5个类(IModel,Model,IView,View和Presenter),每个类都有相同的基本名称,但位于不同的文件夹中。文件夹结构类似于
+Models
+Interfaces
+ IModelOne
+ ModelOne
+ SubsetOne
+ Interfaces
+ IModelThree
+ ModelThree
+Views
+ Interfaces
+ IViewOne
+ SubsetOne
+ Interfaces
+ IViewThree
+ ViewThree
+Presenters
+ PresenterOne
+ SubsetOne
+ PresenterThree
“模型”,“视图”和“演示者”文件夹具有相同的结构。我想在同一结构中添加模板中的新项目,基于1.)他们点击了Add菜单项(例如,如果他们右键单击Presenters-> SubsetOne并选中)添加新项,它会以某种方式意识到SubsetOne是"基本路径"来构建来自?)的所有内容,或2.)允许用户输入"基本路径"通过向导型的东西。
在不使用向导的情况下,我可以在模型,视图和演示者文件夹(或某些硬编码子目录)的顶层创建所有必需的文件。对于某些小部件(那些需要处于最高级别的小部件)来说这是可以的,但它的使用将非常有限。
我按照http://msdn.microsoft.com/en-us/library/ms185301.aspx的说明设置了所有内容。尝试使用向导时遇到的问题是,它弹出一条消息,指示" Value不在预期范围内。"我尝试通过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!