ProjectItem Element (Visual Studio Item Templates)

Specifies a file that is included in the item template.

备注

The ProjectItem element accepts different attributes depending on whether the template is for a project or an item. This topic explains the ProjectItem element for item. For an explanation of the ProjectItem element for project templates, see ProjectItem 元素(Visual Studio 项目模板).

<VSTemplate>
   <TemplateContent>
     <ProjectItem>

<ProjectItem
    SubType="Form/Component/CustomControl/UserControl"
    CustomTool="string"
    ItemType="string"
    ReplaceParameters="true/false"
    TargetFileName="TargetFileName.ext">
        FileName.ext
</ProjectItem>

特性和元素

The following sections describe attribute, child elements, and parent elements.

特性

Attribute

Description

SubType

Optional attribute.

Specifies the subtype of an item in a multi-file item template. This value is used to determine the editor that Visual Studio will use to open the item.

CustomTool

Optional attribute.

Sets the CustomTool for the item in the Project file.

ItemType

Optional attribute.

Sets the ItemType for the item in the Project file.

ReplaceParameters

Optional attribute.

A Boolean value that specifies whether the item has parameter values that must be replaced when a project is created from the template. Default value is false.

TargetFileName

Optional attribute.

Specifies the name of the item that is created from the template. This attribute is useful for using parameter replacement to create an item name.

子元素

None.

父元素

Element

Description

TemplateContent

Specifies the contents of the template.

文本值

A text value is required.

A string that represents the name of a file in the template .zip file.

备注

ProjectItem is an optional child of TemplateContent.

The TargetFileName attribute can be used to rename files with parameters. For example, if the file MyFile.vb exists in the root directory of the template .zip file, but you want the file to be named based on the file name provided by the user in the Add New Item dialog box, you would use the following XML:

<ProjectItem TargetFileName="$fileinputname$.vb">MyFile.vb</ProjectItem>

When an item is created from this template, the file name will be based on the name the user entered in the Add New Item dialog box. This is useful when creating multi-file item templates. For more information, see 如何:创建多文件项模板 and 模板参数.

示例

The following example illustrates the metadata for the standard item template for a Visual C# class.

<VSTemplate Type="Item" Version="3.0.0"
    xmlns="https://schemas.microsoft.com/developer/vstemplate/2005">
    <TemplateData>
        <Name>MyClass</Name>
        <Description>My custom C# class.</Description>
        <Icon>Icon.ico</Icon>
        <ProjectType>CSharp</ProjectType>
        <DefaultName>MyClass.cs</DefaultName>
    </TemplateData>
    <TemplateContent>
        <ProjectItem ReplaceParameters="true">MyClass.cs</ProjectItem>
    </TemplateContent>
</VSTemplate>

请参见

任务

如何:创建多文件项模板

参考

Visual Studio 模板架构参考

概念

在 Visual Studio 中创建项目和项的模板

模板参数