gpt4 book ai didi

templates - 多个项目中的多文件项模板

转载 作者:行者123 更新时间:2023-12-03 17:48:22 24 4
gpt4 key购买 nike

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

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

这就是我要找的:

解决方案

  • 项目一
  • 模板项目 1
  • 项目二
  • 模板项目 2
  • 最佳答案

    好的,基于 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.Evaluation.ProjectCollection 中的 Microsoft.Build dll 以编程方式将其添加到项目中。
    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 中的 Assets 中。此外,我将项目模板项目添加为文件,并在 bin 中构建项目项目后提供由 VS 创建的 zip 文件。

    关于templates - 多个项目中的多文件项模板,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28077902/

    24 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com