我正在编写一个 Visual Studio 项目项模板,在这种情况下,我将只使用一个 xml 文件。我今天的问题是我想将文件名传递到完成的 xml 文件中。即...当我为模板命名文件名时,它会创建项目文件并将其作为 xml 文件添加到我的项目中。但我希望发生的是,当它创建 xml 文件时,它会将文件名值传递到 xml 文件中。
为了更好地解释它,我现在有一个项目模板,其中模板 xml 有这样一行:
<!-- Created by ModBuddy on $time$ -->
上面是在实际的 VS 项目模板中,所以当我创建一个项目添加到我的项目时,它实际上创建了我的 xml 文件,其中包含以下行。
<!-- Created by ModBuddy on 10/26/2014 1:16:16 AM -->
本质上,它在创建 xml 文件时将当前日期时间从项目模板传递到我的 xml 文件。我的问题是您如何定义和/或传递 $time$ 等参数,我在哪里可以找到有关这样做的更多信息。我希望能够将文件的文件名从 Visual Studio 项目模板(我将自己创建)传递到工作的 xml 文件中,类似于 $filename$ 但我不知道如何执行此操作或在哪里可以了解更多信息。
最佳答案
经过大量研究,我发现您可以使用以下格式创建和传递自己的自定义参数
$whatevernameyoulike$
通过使用 Visual Studio 自定义向导。基本上,您创建一个新的 VS 项目、类库并从程序集中实现 IWizard 接口(interface):
Microsoft.VisualStudio.TemplateWizardInterface
您还必须在程序集中为您稍后将创建的项目或项目模板创建一个界面/WindowsForm。然后修改实现 IWizard 的类中的 RunStarted 方法以像这样传递参数:
public void RunStarted(object automationObject, Dictionary<string, string> replacementsDictionary, WizardRunKind runKind, object[] customParams)
{
GeneralInfoPage generalInfoPage = new GeneralInfoPage();
ExtendedInfoPage extendedInfoPage = new ExtendedInfoPage();
ModWizardMenu modHost = new ModWizardMenu();
modHost.Pages.Add(generalInfoPage);
modHost.Pages.Add(extendedInfoPage);
if (modHost.ShowDialog() != DialogResult.OK)
{
throw new WizardCancelledException("The wizard has been canceled by the user.");
}
string modDescription = generalInfoPage.ModDescription;
string toParse = modDescription;
if (toParse.Length > 127)
toParse = toParse.Substring(0, 127);
createUnit = extendedInfoPage.createUnit.Checked;
createBuilding = extendedInfoPage.createBuilding.Checked;
createSponsor = extendedInfoPage.createSponsor.Checked;
replacementsDictionary.Add("$unitName$", extendedInfoPage.unitName);
replacementsDictionary.Add("$buildingName$", extendedInfoPage.buildingName);
replacementsDictionary.Add("$sponsorName$", extendedInfoPage.sponsorName);
}
这里的主要部分是replacementsDictionary,当然这只是解决方案的一部分。接下来,您必须创建自定义、项目或项模板以访问新创建的向导程序集。
创建 VS 项目或项目模板后,您可以从创建的 zip 文件中提取文件并修改 .vstemplate 文件以添加对自定义向导程序集的引用,如下所示:
<WizardExtension>
<Assembly>rhodesMBWizard, Version=1.0.0.0, Culture=Neutral, PublicKeyToken=56e9113a0fdbb124</Assembly>
<FullClassName>rhodesMBWizard.InfoManager</FullClassName>
</WizardExtension>
确保您的程序集具有正确的公共(public) token key 很重要,如果您像我一样不断更改类名,您将需要确保我拥有 rhodesMBWizard.InfoManager 的位置应该是:
yourAssemblyNamespace.classnameThatImplementsIWizard
如果您查看 vstemplate 文件,您将看到:
<TemplateContent>
<References />
<ProjectItem TargetFileName="$fileinputname$.xml" ReplaceParameters="true">BuildingTemplate.xml</ProjectItem>
</TemplateContent>
以 ProjectItem 开头的行是提取命名参数并将它们传递到该行末尾的项目或项模板文件名的行,在我的例子中是 BuildingTemplate.xml。
所以在我的 BuildingTemplate.xml 文件中我会有一堆
$buildingName$
条目,项目或项目模板拉取值
$buildingName$
在我创建的 CustomWizard 中的 Replacements Dictionary 中,它会在我的项目中为我创建一个工作 xml 文件,其中所有参数都已更改。
现在做所有这一切的要点是,当您创建项目或项目模板时,您可以以前面提到的用户界面或 WindowsForm 的形式接收用户输入,允许用户做出选择并预先命名一些东西这将减少工作 xml 文件中所需的工作量。
关于xml - 将参数从 Visual Studio 项目模板传递到生成的项目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30035807/