代码模板的使用

18 篇文章 0 订阅
16 篇文章 0 订阅


一、代码模板的基本概念
所谓“代码模板”,就是已经有一部分代码的源程序文件,
在添加新的Class文件或者Form文件至Project时,
VS.NET IDE将复制这些模板文件为一个新文件,
新文件的文件名就是在Add New Item对话框中输入的文件名。
VS.NET IDE还能自动将模板文件中的Class Name更改为新类名。

使用代码模板,可以提高一些典型编码工作的工作效率,比如:
(1)自动添加一段注释块,用于记录作者、创建日期、维护历史等。
(2)自动添加Imports语句。

二、最简单的代码模板
加入希望每个类文件中都包括一段声明版权的代码,可按如下步骤操作:
(1)用Windows资源管理器浏览到如下路径:
C:/Program Files/Microsoft Visual Studio .NET 2003/Vb7/VBWizards/Class/Templates/1033
(2)找到Class.vb文件,用记事本打开,其内容应为:
Public Class [!output SAFE_ITEM_NAME]
End Class

(3)修改其内容为:

'
' Copyright (c) 2004 GrapeCity inc. All rights reserved.
'
Public Class [!output SAFE_ITEM_NAME]
End Class

(4)保存并关闭
(5)启动VS.NET,创建一个新的Windows Application Project
(6)添加新的类文件Class1.vb。
这个新的Class中已经包含有版权声明注释代码。

三、更复杂的代码模板
上面的方法将导致任何项目的新Class File都受影响。
如果希望提供更多灵活性,允许开发人员有选择地使用,
可以创建自定义的代码模板,使其作为一个新类别出现在Add New Item对话框中。
具体步骤如下:

转帖自 http://support.microsoft.com/?kbid=870715

This step-by-step article describes how to create a custom code template in a Microsoft Visual C# .NET Windows application project and then add it to the Visual Studio .NET IDE.

To create a custom code template in a Microsoft Visual C# .NET Windows application project and then add it to the Visual Studio .NET IDE, follow these steps.

Change the LocalProjectItems.vsdir file

Right-click My Computer, and then click Explore.
Locate the LocalProjectItems folder.

In Microsoft Visual Studio .NET 2003, the folder is located in the following path:
C:/Program Files/Microsoft Visual Studio .NET 2003/VC#/CSharpProjectItems/LocalProjectItems

In Microsoft Visual Studio .NET, the folder is located in the following path::
C:/Program Files/Microsoft Visual Studio .NET/VC#/CSharpProjectItems/LocalProjectItems

Note C represents the hard disk drive where you installed Visual Studio .NET.
Create a new MyCustomTemplates folder in the LocalProjectItems folder.
Copy the LocalProjectItems.vsdir file to the MyCustomTemplates folder.

In Microsoft Visual Studio .NET 2003, the LocalProjectItems.vsdir file is located in the following path:
C:/Program Files/Microsoft Visual Studio .NET 2003/VC#/CSharpProjectItems/LocalProjectItems

In Microsoft Visual Studio .NET, the LocalProjectItems.vsdir file is located in the following path:
C:/Program Files/Microsoft Visual Studio .NET/VC#/CSharpProjectItems/LocalProjectItems

5.?In the MyCustomTemplates folder, open the LocalProjectItem.vsdir file by using Notepad, and then replace the existing text with the following text:

../../MyCustomTemplatesWinFormWiz.vsz|{FAE04EC1-301F-11d3-BF4B-00C04F79EFBC}|#2237|10|#2264|{FAE04EC1-301F-11d3-BF4B-00C04F79EFBC}|4535|0|Form.cs

6.?Save the file, and then quit Notepad.

back to the top

Create a custom Windows form wizard

1.?Open the CSharpProjectItems folder.

In Visual Studio .NET 2003, the folder is located in the following path:
C:/Program Files/Microsoft Visual Studio .NET 2003/VC#/CSharpProjectItems

In Visual Studio .NET, the folder is located in the following path:
C:/Program Files/Microsoft Visual Studio .NET/VC#/CSharpProjectItems

2.?Copy the CSharpAddWinFormWiz.vsz file to the CSharpProjectItems folder, and then rename the file to MyCustomTemplatesWinFormWiz.vsz.
3.?Open the MyCustomTemplatesWinFormWiz.vsz file by using Notepad, and then change the wizard name parameter value to the following:

Param="WIZARD_NAME = MyCustomTemplatesWinFormWiz"

4.?Save the file, and then quit Notepad.

Create a custom Windows form template

1.?Open the VC#Wizards folder.

In Visual Studio .NET 2003, the folder is located in the following path:
C:/Program Files/Microsoft Visual Studio .NET 2003/VC#/VC#Wizards

In Visual Studio .NET, the folder is located in the following path:
C:/Program Files/Microsoft Visual Studio .NET/VC#/VC#Wizards

2.?Copy the CSharpAddWinFormWiz folder to the VC#Wizards folder, and then change the name of the the CSharpAddWinFormWiz folder to MyCustomTemplatesWinFormWiz.
3.?Double-click the MyCustomTemplatesWinFormWiz folder, open the Templates folder, and then double-click the 1033 folder.
4.?Open the NewWinForm.cs file by using Notepad.
5.?Add the lines of code that you use frequently to the top of the NewWinForm.cs file.
6.?Save the file, and then quit Notepad.
?
Open the template in Visual Studio .NET

1.?Start Visual Studio .NET.
2.?On the File menu, point to New, and then click Project.
3.?Under Project Types, click Visual C# Projects.
4.?Under Templates, click WindowsApplication.
5.?Click OK. By default, the Form1 form appears.
6.?On the Project menu, click Add New Item. The Add New Item - Your Windows Application dialog box appears.
Note Your Windows Application is a placeholder for the name of the Windows application that you created.
7.?Under Categories, expand Local Project Items, click MyCustomTemplates, and then click Open. By default, the Form2.cs file is created.
8.?Right-click the Form2 form, and then click View Code.
9.?The code that you see is the code that you added in step 5 of the "Create a custom Windows form template" section.

REFERENCES
For more information, visit the following Microsoft Web site:
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vsent7/html/vxoriEnterpriseTemplates.asp

?

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值