Using CustomProperties of CodeSmith

來源:互聯網
上載者:User
     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] %>
<% } %>

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.