好的,基于 VS 2017 我创建了一个 VSIX 项目,一个库项目和一个项目模板项目
1- 在项目模板项目中,我创建项目模板文件一和二。我还像这样在 vstemplate 文件中添加了wizardExtension 信息
<TemplateContent>
<ProjectItem ReplaceParameters="true" TargetFileName="$fileinputname$Service.cs">Service.cs</ProjectItem>
<ProjectItem ReplaceParameters="true" TargetFileName="$fileinputname$DTO.cs">DTO.cs</ProjectItem>
<ProjectItem ReplaceParameters="true" TargetFileName="$fileinputname$Message.cs">Message.cs</ProjectItem>
</TemplateContent>
<WizardExtension>
<Assembly>WizardImplementation, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null</Assembly>
<FullClassName>WizardImplementation.WizardImplementation</FullClassName>
</WizardExtension>
WizardImplementation 是我的类库项目。
2- 我将 TemplateWizardInterface Nuget 包添加到类库项目并在类中实现 IWizard。在 ProjectItemFinishedGenerating 中,我将第二个模板项(如文件)移动到第二个项目位置文件夹,并使用 Microsoft.Build dll 中的 Microsoft.Build.Evaluation.ProjectCollection 以编程方式将其添加到项目中。
public void ProjectItemFinishedGenerating(ProjectItem projectItem)
{
if (projectItem.FileNames[0].Contains("DTO") || projectItem.FileNames[0].Contains("Message"))
{
string newPath = Path.GetFullPath(Path.Combine(projectItem.FileNames[0], @"..\Project2\"));
if (!Directory.Exists(newPath))
{
MessageBox.Show($"Message path does not exist. \r\n {newPath}");
}
else
{
var newFullPath = Path.Combine(newPath, projectItem.Name);
File.Move(projectItem.FileNames[0], newFullPath);
var p = Microsoft.Build.Evaluation.ProjectCollection.GlobalProjectCollection.GetLoadedProjects(PathInformation.MessageProjectPath).FirstOrDefault();
if (p == null)
p = new Microsoft.Build.Evaluation.Project(PathInformation.MessageProjectPath);
var res = p.AddItem("Compile", newFullPath);
p.Save();
if(res == null)
{
MessageBox.Show("Nothing added to project");
}
else
{
MessageBox.Show($"{res.Count()} item added to project");
}
}
}
}
有关 IWizard 的更多信息,请查看此链接
https://msdn.microsoft.com/en-us/library/ms185301.aspx
3- 在 VSIX 项目中,我将我在第二步中创建的类库按程序集类型添加到了 vsixmanifest 中。此外,我将项目模板项目添加为文件,并在 bin 中构建项目项目后提供由 VS 创建的 zip 文件。