【问题标题】:Creating Visual Studio Templates创建 Visual Studio 模板
【发布时间】:2008-11-03 01:34:48
【问题描述】:

我希望创建一个 Visual Studio 2008 模板,该模板将创建一个基本项目,并根据用户输入的选项删除某些文件/文件夹。

现在,我已经在网上学习了一些教程,这些教程让我创建了表单来查询用户并将数据传递给 IWizard 类,但我不知道从那里做什么。

教程提供了一个示例来进行一些简单的替换: 代码:

Form1 form = new Form1();
DialogResult dlg = form.ShowDialog();
if (dlg == DialogResult.OK)
{
    foreach (KeyValuePair<string, string> pair in form.Parameters)
    {
        if (!replacementsDictionary.ContainsKey(pair.Key))
            replacementsDictionary.Add(pair.Key, pair.Value);
        else
            replacementsDictionary[pair.Key] = pair.Value;
    }
}
form.Close();

但我希望根据用户设置有选择地包含文件,如果可能的话,根据设置有选择地在文件中包含代码部分。

有没有聪明的方法来做到这一点,还是我必须手动删除 IWizard:ProjectFinishedGenerating() 中的项目文件?

【问题讨论】:

    标签: c# visual-studio templates


    【解决方案1】:

    根据我的经验,ShouldAddProjectItem 只会在模板项目中为 文件夹 调用。因此,它几乎没有用。

    相反,您需要将代码放入使用 VS API 删除 ProjectItems 的 ProjectFinishedGenerating 实现中。

    在那里,您可以像这样删除项目:

    ProjectItem file = project.ProjectItems.Item("File.cs");
    file.Remove();
    

    【讨论】:

      【解决方案2】:

      您可以通过使用 $if$ 和替换来选择性地包含文件的某些部分。例如,请参阅默认 C# 类库模板中的此位:

      <ItemGroup>
          <Reference Include="System"/>
          $if$ ($targetframeworkversion$ >= 3.5)
          <Reference Include="System.Core"/>
          <Reference Include="System.Xml.Linq"/>
          <Reference Include="System.Data.DataSetExtensions"/>
          $endif$
      

      ...等等。

      【讨论】:

        【解决方案3】:

        如果我理解正确,您希望能够确定是否应该将项目项添加到项目中。

        如果是这样,您可以实现 IWizard.ShouldAddProjectItem 并返回是否要添加文件。

        【讨论】:

        • 这很有帮助,但我也想知道是否可以根据设置选择性地包含文件的某些部分。不过,您在这里提供的答案很棒。谢谢。
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-02-06
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-04-27
        相关资源
        最近更新 更多