【问题标题】:Visual studio item template $safeitemname$ not working as expectedVisual Studio 项目模板 $safeitemname$ 未按预期工作
【发布时间】:2018-10-16 01:09:33
【问题描述】:

我正在创建一个 Visual Studio 项目模板来创建一些依赖于“容器”文件的文件。

最后一个文件<ProjectItem SubType="Code" TargetFileName="$fileinputname$\I$fileinputname$ View.cs" ReplaceParameters="true">Container View.cs</ProjectItem> 创建了一个需要特定模型类型的视图界面。 但是$safeitemname$ 参数没有像我预期的那样工作。

输出Container View.cs文件:

public interface IIMy_Triplet_View : IView<IMy_Triplet_View_Model>
{
}

预期:

public interface IMy_Triplet_View : IView<My_Triplet_Model>
{
}

这是 Container View.cs 模板的来源:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using WebFormsMvp;

namespace $rootnamespace$
{
    public interface $safeitemname$_View : IView<$safeitemname$_Model>
    {
    }
}

还有 .vstemplate 文件

    <VSTemplate Version="3.0.0" xmlns="http://schemas.microsoft.com/developer/vstemplate/2005" Type="Item">
      <TemplateData>
        <DefaultName>Model-View-Presenter</DefaultName>
        <Name>Model-View-Presenter</Name>
        <Description>Creates a model-view-presenter triplet</Description>
        <ProjectType>CSharp</ProjectType>
        <SortOrder>10</SortOrder>
        <Icon>__TemplateIcon.png</Icon>
        <PreviewImage>__PreviewImage.png</PreviewImage>
      </TemplateData>
      <TemplateContent>
        <References>
          <Reference>
            <Assembly>WebFormsMvp</Assembly>
          </Reference>
        </References>
        <ProjectItem SubType="Code" TargetFileName="$fileinputname$" ReplaceParameters="false">Container</ProjectItem>
        <ProjectItem SubType="Code" TargetFileName="$fileinputname$\$fileinputname$ Model.cs" ReplaceParameters="true">Container Model.cs</ProjectItem>
        <ProjectItem SubType="Code" TargetFileName="$fileinputname$\$fileinputname$ Presenter.cs" ReplaceParameters="true">Container Presenter.cs</ProjectItem>
        <ProjectItem SubType="Code" TargetFileName="$fileinputname$\I$fileinputname$ View.cs" ReplaceParameters="true">Container View.cs</ProjectItem>
      </TemplateContent>
    </VSTemplate>

【问题讨论】:

    标签: c# visual-studio parameters visual-studio-templates


    【解决方案1】:

    在这里找到解决方案:http://social.msdn.microsoft.com/Forums/en-US/vsx/thread/8516524a-22d3-4ed2-b2da-aaafe855fb92/

    添加一个自定义参数(TemplateContent 下的最后一项):

    <VSTemplate Version="3.0.0" ... Type="Item">
     <TemplateData>
      ...
     </TemplateData>
     <TemplateContent>
      ...
      <CustomParameters>
       <CustomParameter Name="$basename$" Value="$fileinputname$"/>
      </CustomParameters>
     </TemplateContent>
    </VSTemplate>
    

    并在文件中使用 $basename$ 而不是 $fileinputname$。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多