The Host Item Node in VSTA Projects

Within a VSTA project there is a Host Item Node.  This displays in the Project Explorer as a subfolder within the VSTA project and contains the main code file as well as any DPM files.  By default the folder's name is the HostID and the icon is the open or closed folder icon.  These settings can be changed in the project template under the Host node in the ProjectExtensions section. 

 

VSTA host item specific code files are stored here so that they may be accessed through IVstaHostAdapter.ProjectHostItems.  The SDK samples demonstrate this in the Macro Recording and Dynamic Programming Model samples (see code below).

 

<!-- This section defines properties that apply to HostItems.

 

      Host - Properties of the folder node in the project.

      Name - The display name of the folder node.

      GeneratedCodeNamespace - Namespace of code files contained in the folder node.

                               Used by the Properties window in Project Explorer.

      IconIndex - 0-based index in the IconImageList of the icon for this node.

      OpenFolderIconIndex - 0-based index in the IconImageList of the open folder icon for this node.

 -->

         <Host

             Name="ShapeAppCSharp AddIn Folder"

             GeneratedCodeNamespace="$safeprojectname$"

             IconIndex=""

             OpenFolderIconIndex="">

             <HostItem

                 Name="AppAddIn"

                 Code="AppAddIn.cs"

                 CanonicalName="AppAddIn"

                 Blueprint="AppAddIn.Designer.xml"

                 GeneratedCode="AppAddIn.designer.cs"

                 IconIndex="" />

         </Host>

 

 

Macro Recording Sample:

internal void SaveMacro(System.CodeDom.CodeMemberMethod method)

{

    if (this.macroProject == null)

    {

        if (!ShowIde(false))

            return;

    }           

    IVstaHostAdapter hostAdapter = (IVstaHostAdapter)this.macroProject.get_Extender("VSTAHostAdapter2007");

 

    string hostItemFileName = hostAdapter.ProjectHostItems[0].CodeFilePath;

    string hostItemClassName = hostAdapter.ProjectHostItems[0].ProgrammingModelHostItem.Identifier;

    EnvDTE.ProjectItem hostItemProjectItem =

        this.macroProject.ProjectItems.Item(Path.GetFileName(hostItemFileName));

    EnvDTE.CodeClass hostItemClass = FindClass(hostItemClassName, hostItemProjectItem.FileCodeModel.CodeElements);

 

    method.Name = GenerateNewMacroName(hostItemClass);

 

    hostItemProjectItem.Open("{7651A701-06E5-11D1-8EBD-00A0C90F26EA}");

 

    hostAdapter.ProjectHostItems[0].AddMethod(method);

    hostItemProjectItem.Save(null);

 

    this.macroProject.DTE.Solution.SolutionBuild.Build(true);

}

 

 

Dynamic Programming Model Sample

private IVstaProjectHostItem AddProjectHostItemInternal(string itemName, string templateName, string itemEntryPoint)

{

    foreach (IVstaProjectHostItem existingItem in this.hostAdapter.ProjectHostItems)

    {

        if (existingItem.ProgrammingModelHostItem.Cookie == itemName)

            return null; // Do not add existing one.

    }

 

    // Make sure the template is not read-only.

    string languageFolder = null;

    string languageExtension = null;

    if (this.language == SupportedLanguage.CSharp)

    {

        languageFolder = "CSharp";

        languageExtension = ".cs";

    }

    else

    {

        languageFolder = "VisualBasic";

        languageExtension = ".vb";

    }

    string templatePath = this.hostItemTemplatesPath + @"\" + languageFolder + @"\" + templateName + languageExtension;

    FileInfo fileInfo = new FileInfo(templatePath);

    bool isReadOnly = fileInfo.IsReadOnly;

    fileInfo.IsReadOnly = false;

    IVstaProjectHostItem projectHostItem = null;

    try

    {

        projectHostItem = hostAdapter.ProjectHostItems.AddProjectHostItem(

                            itemName,

                            "Microsoft.VisualStudio.Tools.Applications.Samples.ShapeApp." + itemEntryPoint,

                            "Microsoft.VisualStudio.Tools.Applications.Runtime.IEntryPoint",

                            itemName,

                            this.hostItemTemplatesPath + @"\" + languageFolder + @"\" + templateName);

        this.project.Save(this.project.FileName);

 

    }

    finally

    {

        fileInfo.IsReadOnly = isReadOnly;

    }

    if (projectHostItem != null)

        return projectHostItem;

    else

        throw new InvalidOperationException("Fail to add project host item.");

 

}

 

 

 


Posted May 26 2009, 12:39 PM by Melody
Filed under: ,
Copyright Summit Software Company, 2008. All rights reserved.