您應該可以在該模板中添加IWizard
引用,當您在文件 - >添加窗口中單擊ok時,將會運行該模板。你需要add the assembly and type到vstemplate文件。
執行RunFinished
或可能的ProjectItemFinishedGenerating
方法。然後,您可以使用由Visual Studio公開的EnvDTE
對象來使用標準Visual Studio Extensibility模型處理解決方案中的任何項目。
The following code snippit(來自開放源代碼T4工具箱)顯示如何設置此屬性。
/// <summary>
/// Sets the known properties for the <see cref="ProjectItem"/> to be added to solution.
/// </summary>
/// <param name="projectItem">
/// A <see cref="ProjectItem"/> that represents the generated item in the solution.
/// </param>
/// <param name="output">
/// An <see cref="OutputFile"/> that holds metadata about the <see cref="ProjectItem"/> to be added to the solution.
/// </param>
private static void SetProjectItemProperties(ProjectItem projectItem, OutputFile output)
{
// Set "Build Action" property
if (!string.IsNullOrEmpty(output.BuildAction))
{
ICollection<string> buildActions = GetAvailableBuildActions(projectItem);
if (!buildActions.Contains(output.BuildAction))
{
throw new TransformationException(
string.Format(CultureInfo.CurrentCulture, "Build Action {0} is not supported for {1}", output.BuildAction, projectItem.Name));
}
SetPropertyValue(projectItem, "ItemType", output.BuildAction);
}
// Set "Copy to Output Directory" property
if (output.CopyToOutputDirectory != default(CopyToOutputDirectory))
{
SetPropertyValue(projectItem, "CopyToOutputDirectory", (int)output.CopyToOutputDirectory);
}
// Set "Custom Tool" property
if (!string.IsNullOrEmpty(output.CustomTool))
{
SetPropertyValue(projectItem, "CustomTool", output.CustomTool);
}
// Set "Custom Tool Namespace" property
if (!string.IsNullOrEmpty(output.CustomToolNamespace))
{
SetPropertyValue(projectItem, "CustomToolNamespace", output.CustomToolNamespace);
}
}
Thanks!這看起來好像可能有用,但是你碰巧有鏈接到示例或教程?我試圖按照http://msdn.microsoft.com/en-us/library/ms185301.aspx上的教程,但我發現它有點難以處理。我也很難找到控制「複製到輸出目錄」設置的選項。你提到了EnvDTE對象,所以我認爲它包含在EnvDTE.ProjectItem.Properties集合中。這是我第一次看編寫擴展名,看起來這個東西的文檔有點稀疏。 –
從T4Toolbox開源項目中添加代碼示例 – jessehouwing
工作得很好。該代碼示例非常有幫助。謝謝! –