'Item template for shared project in Visual Studio
I've created a custom item template in Visual Studio for a class. I've done all the required steps and the template can be added as a new item in every project type except for the "Shared Project" type. It doesn't show in the list of choices when adding a new item. My goal is to use this template for new classes in the Shared Project.
Here is the .xml file for my template...
<VSTemplate Version="3.0.0" xmlns="http://schemas.microsoft.com/developer/vstemplate/2005" Type="Item">
<TemplateData>
<DefaultName>Class_ODBC.cs</DefaultName>
<Name>Class_ODBC</Name>
<Description>Template for a class using ODBC and System.Data</Description>
<ProjectType>CSharp</ProjectType>
<SortOrder>10</SortOrder>
<Icon>__TemplateIcon.ico</Icon>
<TemplateGroupID>WinRT-Managed</TemplateGroupID>
</TemplateData>
<TemplateContent>
<References />
<ProjectItem SubType="" TargetFileName="$fileinputname$.cs" ReplaceParameters="true">Class_ODBC.cs</ProjectItem>
</TemplateContent>
</VSTemplate>
And the .cs file for the template itself...
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Odbc;
using System.Text;
namespace %rootnamespace%
{
class %safeitemname%
{
#region Fields
#endregion
#region Constructors
#endregion
#region Methods
#endregion
#region Events/Handlers
#endregion
#region Properties
#endregion
}
#region Exceptions
#endregion
}
Any help is greatly appreciated.
Solution 1:[1]
I was trying to achieve the same, documentation didn't provide much help but checked one of the already provided templates that is available within a Shared project and found that it can be done through the <AppliesTo> element in the vstemplate. I'm not sure why VS highlights the tag as incorrect for the schema, as MSDN clearly states it's valid
The final result looks like this
<?xml version="1.0" encoding="utf-8"?>
<VSTemplate Version="3.0.0" Type="Item" xmlns="http://schemas.microsoft.com/developer/vstemplate/2005" xmlns:sdk="http://schemas.microsoft.com/developer/vstemplate-sdkextension/2010">
<TemplateData>
<Name>Custom ViewModel</Name>
<Description>Custom ViewModel</Description>
<Icon>MyViewModelTemplate.ico</Icon>
<TemplateID>6dde10d5-17ae-4cf1-9880-dc448eca638d</TemplateID>
<TemplateGroupID>WinRT-Managed</TemplateGroupID>
<AppliesTo>CSharp</AppliesTo>
<ProjectType>CSharp</ProjectType>
<SortOrder>1</SortOrder>
<RequiredFrameworkVersion>4.5</RequiredFrameworkVersion>
<NumberOfParentCategoriesToRollUp>1</NumberOfParentCategoriesToRollUp>
<DefaultName>ViewModel.cs</DefaultName>
<TargetPlatformName>Windows</TargetPlatformName>
<RequiredPlatformVersion>8</RequiredPlatformVersion>
</TemplateData>
<TemplateContent>
<ProjectItem SubType="Code" TargetFileName="$fileinputname$.cs" ReplaceParameters="true">Class.cs</ProjectItem>
</TemplateContent>
</VSTemplate>
Hope this helps.
Solution 2:[2]
After digging around in Visual Studio's installed templates, I discovered that to add a template to a sub-category, such as Shared Project under C#, one needs to create a folder with the same name as the sub-category and place the template in that folder.
The "AppliesTo" tag seems to only apply to the top-level categories.
Solution 3:[3]
The issue I discovered is that you need to have two tags:
<AppliesTo>CSharp</AppliesTo>
<TemplateID>YourTemplate.ID</TemplateID>
The AppliesTo tag by itself did not work. Adding the TemplateID did the trick, even though it's not required for non-shared projects.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Diego Malanij |
| Solution 2 | |
| Solution 3 | Russell Bearden |
