CodeSmith实用技巧(二):使用FileNameEditor

FileNameEditor类给我们提供了在CodeSmith属性面板中弹出打开或保存文件对话框的方式,在使用时,首先在模版中得添加对程序集CodeSmith.CustomProperties的引用。然后就可以在模版中定义一个属性来使用FileNameEditor

 1<script runat="template">
 2
 3private string _userFileName = @"c:\temp\test.txt";
 4
 5 
 6
 7[Editor(typeof(FileNameEditor), typeof(System.Drawing.Design.UITypeEditor)),
 8
 9Category("Custom"), Description("User selected file.")]
10
11 
12
13public string UserFileName
14
15{
16
17      get {return _userFileName;}
18
19      set {_userFileName= value;}
20
21}

22
23</script>
24
25

当我们执行该模版时,在属性面板中同样显示为一个按钮:

单击该按钮,弹出一个保存文件的对话框:

我们也可以通过FileDialogAttribute来自定义弹出的对话框,修改模版为:

 1private string _openFileName = @"c:\temp\test.txt";
 2
 3
 4[Editor(typeof(FileNameEditor), typeof(System.Drawing.Design.UITypeEditor)),
 5
 6FileDialogAttribute(FileDialogType.Open, Title="Select Input File"),
 7
 8Category("Custom"), Description("User selected file.")]
 9
10 
11
12public string OpenFileName
13
14{
15
16      get {return _openFileName;}
17
18      set {_openFileName= value;}
19
20}

21
22

弹出的对话框如下所示:

当我们想用一个文件夹的名称来代替文件时,可以使用FolderNameEditor类。

 1<%@ Assembly Name="System.Design" %>
 2<script runat="template">
 3private string _outputDirectory = @"c:\temp";
 4[Editor(typeof(System.Windows.Forms.Design.FolderNameEditor), typeof(System.Drawing.Design.UITypeEditor)),
 5Category("Custom"), Description("Output directory.")]
 6public string OutputDirectory
 7{
 8       get {return _outputDirectory;}
 9       set {_outputDirectory= value;}
10}

11</script>
12
13

FileNameEditor重要方法和属性介绍:

公共方法:

名称

描述

EditValue

使用由 GetEditStyle 方法提供的编辑器样式编辑指定的对象

GetEditStyle

获取 EditValue 方法所使用的编辑样式

 

作者:TerryLee
出处:http://terrylee.cnblogs.com
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。
posted @ 2005-12-27 13:23 TerryLee 阅读(1772) 评论(2)  编辑 收藏 所属分类: CodeSmith

  回复  引用  查看    
#1楼 2006-08-17 06:47 | microshot      
vs2003的对应属性是什么呢?
  回复  引用  查看    
#2楼 2006-08-17 07:05 | microshot      
哦,没有加这句话导致的:)
<%@ Assembly Name="System.Design" %>


标题  
姓名  
主页
Email (博主才能看到) 
验证码 *  看不清,换一张 [登录][注册]
内容(请不要发表任何与政治相关的内容)  
  博客园首页

  新闻频道

  社区

  小组

  博问

  网摘

  闪存

  登录  使用高级评论  新用户注册  返回页首  恢复上次提交      
该文被作者在 2005-12-28 08:28 编辑过
成果网帮您增加网站收入


相关链接: