Adding a Hidden File to a Project Template

In many situations you want to add background code to a project template without exposing it to the end user. An easy way to do this is to add a hidden code file to the template. Here are the 6 steps to do this which are the same for both C# and VB templates:

1)  Unzip the existing project template.

2)  Add the file to use as the hidden code file to the same folder as all other files in the template (do not put it in a sub folder).

3)  Update the vstemplate file to include the hidden file.  Use the same syntax as the main code file.

4)  Update the xxproj file to include the hidden file.  Use similar syntax as the hidden xml designer file.

5)  Save and re-zip the updated template, moving the new zip file to the ProjectTemplatesLocation.

6)  Run vsta setup for the host.

 

Attached are sample templates updated with a hidden code file for the ShapeAppCSharp SDK samples.  To use these templates, place them in the ProjectTemplatesLocation, run setup, then create a new add-in project based off the templates.

A video covering this sample is also available.

 

Project Explorer with hidden file (ShowAll turned on):

Step 4- vstemplate update
<TemplateContent>
    <Project
        File="ShapeAppCSharpAppAddInProject.vbproj"
        ReplaceParameters="true">
      <!--Main code file-->
      <ProjectItem
            ReplaceParameters="true"
            TargetFileName="AppAddIn.vb">AppAddIn.vb</ProjectItem>
      <!--Add the hidden item-->
      <ProjectItem
          ReplaceParameters="true"
          TargetFileName="HiddenFile.vb">HiddenFile.vb</ProjectItem>

 Step 5- xxproj file update
<ItemGroup>
  <!--Hidden designer file-->
  <None
      Include="AppAddIn.designer.xml">
    <DependentUpon>AppAddIn.vb</DependentUpon>
  </None>
  <!--Add the hidden item under the AppAddIn.cs file-->
  <Compile
      Include="HiddenFile.vb">
    <DependentUpon>AppAddIn.vb</DependentUpon>
  </Compile>

Examples
VB
Hidden code file:
Partial Public Class AppAddIn
    Default Public ReadOnly Property Item(ByVal index As Integer) As Drawing
        Get
            Return Me.Document.Drawings(index)
        End Get
    End Property
End Class

Add-in using code from hidden code file:
Private Sub AppAddIn_Startup(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Startup
    Me(0).Shapes.Add(Me.AvailableShapes(1).Clone())
End Sub

 

 

C#
Hidden code file
using Microsoft.VisualStudio.Tools.Applications.Samples.ShapeApp; 

 

namespace ShapeAppCSharpAppAddIn1{

    public partial class AppAddIn{
        public Drawing this[int index]
        {
            get { return this.Document.Drawings[index]; }
        }
    }
}

 
Add-in using code from hidden code file:
public partial class AppAddIn{
    private void AppAddIn_Startup(object sender, EventArgs e)
    {
        this[0].Shapes.Add(this.AvailableShapes[1].Clone());
    }
    ...
}

 

 

 

 

 

 


Posted Jan 22 2009, 11:00 AM by Melody
Filed under: ,
Attachment: templates.zip
Copyright Summit Software Company, 2008. All rights reserved.