1、FileNameEditor
FileNameEditor類可以讓我們為使用者提供一個標準的開啟檔案的對話方塊和儲存檔案的對話方塊,當然在使用FileNameEditor之前我們得先匯入CodeSmith.CustomProperties程式集,如下:
<%@ Assembly Name="CodeSmith.CustomProperties" %>
<%@ Import Namespace="CodeSmith.CustomProperties" %>
如何在我們得模板中使用FileNameEditor呢?如下:
<script runat="template">
private string _userFileName = @"D:\TEST\TEST.txt";
[Editor(typeof(FileNameEditor), typeof(System.Drawing.Design.UITypeEditor)),
Category("Custom"), Description("User selected file.")]
public string UserFileName
{
get {return _userFileName;}
set {_userFileName= value;}
}
</script>
這樣,當我們執行我們得模板得時候,就可以看到UserFileName得屬性旁邊有一個開啟檔案對話方塊的按鈕了。
我們還可以通過設定FileDialogAttribute來定義檔案對話方塊出項的起始目錄:
private string _openFileName = @"D:\TEST\TEST.txt";
[Editor(typeof(FileNameEditor), typeof(System.Drawing.Design.UITypeEditor)),
FileDialogAttribute(FileDialogType.Open, Title="Select Input File"),
Category("Custom"), Description("User selected file.")]
public string OpenFileName
{
get {return _openFileName;}
set {_openFileName= value;}
}
這樣,當你開啟檔案對話方塊時,系統會直接定位到 D:\TEST\TEST.txt。
2、StringCollection
這個屬性讓我們能為使用者提供輸入一字串列表的功能,使用這個屬性同樣要先匯入CodeSmith.CustomProperties程式集,然後添加這樣的屬性設定即可。<%@ Property Name="List" Type="CodeSmith.CustomProperties.StringCollection" Category="Custom" Description="This is the list." %>
然後我們執行模板的時候就會有List 這樣的屬性,我們點擊旁邊的按鈕,在彈出的String Collection Editor中就可以輸入我們需要的字串列表,按斷行符號表示一個字串結束。
在代碼中按照下面的方法使用輸入的List:
<% for (int i = 0; i < List.Count; i++) { %>
<%= List[i] %>
<% } %>