Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Visual studio item template $safeitemname$ not working as expected

I'm creating a Visual Studio Item template to create a few files which depend on a 'container' file.

The last file <ProjectItem SubType="Code" TargetFileName="$fileinputname$\I$fileinputname$ View.cs" ReplaceParameters="true">Container View.cs</ProjectItem> creates a view interface that expects a certain model type. However the $safeitemname$ parameter didn't work as I expected.

Output Container View.cs file:

public interface IIMy_Triplet_View : IView<IMy_Triplet_View_Model>
{
}

Expected:

public interface IMy_Triplet_View : IView<My_Triplet_Model>
{
}

This is the source for the Container View.cs template:

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

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

And the .vstemplate file

    <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>
like image 560
Ropstah Avatar asked Dec 10 '12 11:12

Ropstah


People also ask

How do I add an item template in Visual Studio Code?

Add an item template to the Add New Item dialog box Create or open a project in Visual Studio. Add an item to the project, and modify it if you want to. Modify the code file to indicate where parameter replacement should take place. For more information, see How to: Substitute parameters in a template. On the Project menu, choose Export Template.

Why is my vstemplate not showing up in Visual Studio 2010?

If the vstemplate file in a template doesn't adhere to the Visual Studio template schema, the template may not appear in the New Project dialog box. If the vstemplate file in a template doesn't adhere to the Visual Studio template schema, the template may not appear in the dialog box where you create new projects.

What are the reserved template parameters for project items?

In the code file for the project item, include parameters where appropriate. For example, the following parameter specifies that the safe project name is used for the namespace in a file: The following table lists the reserved template parameters that can be used by any template: Current version of the common language runtime (CLR).

How do I make an item template only work for certain subtypes?

You can specify that your template should only appear for only certain project subtypes, such as Windows, Office, Database, or Web. Locate the ProjectType element in the .vstemplate file for the item template. Add a ProjectSubType element immediately after the ProjectType element.


1 Answers

Solution found here : http://social.msdn.microsoft.com/Forums/en-US/vsx/thread/8516524a-22d3-4ed2-b2da-aaafe855fb92/

Add a CustomParameter (last item under TemplateContent) :

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

And use $basename$ in the files instead of $fileinputname$.

like image 57
Softlion Avatar answered Dec 02 '22 02:12

Softlion