Solution2.GetProjectItemTemplate 方法
更新:2007 年 11 月
返回所指示的项目项模板的路径。
命名空间: EnvDTE80
程序集: EnvDTE80(在 EnvDTE80.dll 中)
语法
声明
Function GetProjectItemTemplate ( _
TemplateName As String, _
Language As String _
) As String
用法
Dim instance As Solution2
Dim TemplateName As String
Dim Language As String
Dim returnValue As String
returnValue = instance.GetProjectItemTemplate(TemplateName, _
Language)
string GetProjectItemTemplate(
string TemplateName,
string Language
)
String^ GetProjectItemTemplate(
String^ TemplateName,
String^ Language
)
function GetProjectItemTemplate(
TemplateName : String,
Language : String
) : String
参数
TemplateName
类型:System.String模板的名称。
Language
类型:System.String用于编写模板的语言。
返回值
项目项模板的全名。
备注
项目模板存储为 zip 文件。此方法按名称和语言查找项目,并返回模板的路径。
可以按许多不同的方法提供 GetProjectItemTemplate 的参数,如下所示:
以 Language 参数形式传入智能设备 Visual Basic 虚拟项目的 GUID,以 TemplateName 形式传入 zip 文件的名称。
GetProjectItemTemplate("NETCFv2-Class.zip", "{3114F5B0-E435-4bc5-A03D-168E20D9BF83}");
以 Language 参数形式传入智能设备 Visual Basic 虚拟项目的 GUID,以 TemplateName 形式传入字符串“Class”。字符串“Class”从文件夹层次结构派生,它被称作用户界面 (UI) 字符串。其他 UI 字符串有“HTML 页”和“初始屏幕”。UI 字符串与区域设置有关。使用 zip 文件的名称是最安全的传递 TemplateName 参数的方法。
GetProjectItemTemplate("Class", "{3114F5B0-E435-4bc5-A03D-168E20D9BF83}");
以 Language 参数形式传入字符串“VisualBasic”,以 TemplateName 参数形式传入 zip 文件的名称。这之所以有效,是因为 NETCFv2-Class.zip 对于智能设备是唯一的。
GetProjectItemTemplate("NETCFv2-Class.zip", "VisualBasic/SmartDevice-NETCFv2");
还可以为项目项创建自定义模板。若要指定将在其中存储您的模板的目录,请单击“工具”菜单中的“选项”。在“选项”对话框的左侧窗格中,单击“项目和解决方案”。在“Visual Studio 用户项模板位置”框中为您的模板键入路径。或者,您可以接受默认位置。
自定义模板需要唯一的文件名,且不与下面路径中定义的文件名冲突:
<驱动器>:\Program Files\Microsoft Visual Studio 8\Common7\IDE\ItemTemplates\语言。
请确保使用长文件名(相对于 8.3 文件名)。有关更多信息,请参见创建项目模板和项模板。
示例
有关如何运行此外接程序代码的信息,请参见如何:编译和运行自动化对象模型代码示例。
Public Sub OnConnection(ByVal application As Object, _
ByVal connectMode As ext_ConnectMode, ByVal addInInst As Object, _
ByRef custom As Array) Implements IDTExtensibility2.OnConnection
_applicationObject = CType(application, DTE2)
_addInInstance = CType(addInInst, AddIn)
SaveAsExample(_applicationObject)
End Sub
Sub SaveAsExample(ByVal dte As DTE2)
' This add-in adds an HTML page to a solution.
' Open a Visual Basic solution in Visual Studio
' before running this example.
Dim soln As Solution2 = _
CType(_applicationObject.Solution, Solution2)
Dim prj As Project
Dim prjItem As ProjectItem
Dim itemPath As String
Try
prj = soln.Projects.Item(1)
itemPath = soln.GetProjectItemTemplate("HTMLPage.zip", _
"VisualBasic")
' Create a new project item based on the template.
' (In this case, an HTML page.)
prjItem = _
prj.ProjectItems.AddFromTemplate(itemPath, "MyNewHtml")
Catch ex As SystemException
MsgBox("ERROR: " & ex.ToString())
End Try
End Sub
using System.Windows.Forms;
public void OnConnection(object application,
Extensibility.ext_ConnectMode connectMode, object addInInst,
ref System.Array custom)
{
_applicationObject = (DTE2)application;
_addInInstance = (AddIn)addInInst;
// Pass the applicationObject member variable to the code example.
SolnGetProjetItemExample((DTE2)_applicationObject);
}
public void SolnGetProjetItemExample(DTE2 dte)
{
// This add-in adds an item to a Visual Basic solution.
// Open a Visual Basic solution in Visual Studio
// before running this example.
Solution2 soln = (Solution2)_applicationObject.Solution;
Project prj;
ProjectItem prjItem;
string itemPath;
try
{
prj = soln.Projects.Item(1);
itemPath =
soln.GetProjectItemTemplate("HTMLPage.zip", "VisualBasic");
// Create a new project item based on the template.
// (In this case, an HTML page.)
prjItem =
prj.ProjectItems.AddFromTemplate(itemPath, "MyNewHtml");
}
catch (SystemException ex)
{
MessageBox.Show("ERROR: " + ex);
}
}
权限
- 对直接调用方的完全信任。此成员不能由部分信任的代码使用。有关更多信息,请参见通过部分受信任的代码使用库。