【问题标题】:Multi file item template in multiple projects多个项目中的多文件项模板
【发布时间】:2018-01-03 18:00:31
【问题描述】:

我可以制作一个项目模板,将多个文件插入到同一解决方案下的不同项目中吗?

我不想在同一个项目的不同文件夹下添加文件。

这就是我要找的:

解决方案

  • 项目 1

    • 模板项 1
  • 项目 2

    • 模板项 2

【问题讨论】:

  • 我不确定它会如何工作。模板如何知道第 2 项使用哪个项目?
  • 可能只是硬编码,但这就是我试图找出是否可能。
  • 我也在寻找这个问题的答案。
  • 同样,我们有一组非常具体的实体,必须在完整的垂直切片期间创建,如果能够硬编码一些项目/路径以添加文件,那就太好了。

标签: templates visual-studio-2013


【解决方案1】:

好的,基于 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 文件。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-05-08
    • 1970-01-01
    • 2013-01-16
    • 1970-01-01
    • 2017-02-02
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多