FileNameEditor类可以让我们为用户提供一个标准的打开文件的对话框和保存文件的对话框,当然在使用FileNameEditor之前我们得先导入CodeSmith.CustomProperties程序集,如下:
<%
@ Assembly Name="CodeSmith.CustomProperties"
%>
<% @ Import Namespace="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 >
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;}
}
[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.
"
%>
然后我们执行模板的时候就会有L ist 这样的属性,我们点击旁边的按钮,在弹出的String Collection Editor中就可以输入我们需要的字符串列表,按回车表示一个字符串结束。
在代码中按照下面的方法使用输入的List:
<%
for
(
int
i
=
0
; i
<
List.Count; i
++
)
{ %>
<%= List[i] %>
<% } %>
<%= List[i] %>
<% } %>