zoukankan      html  css  js  c++  java
  • CodeSmith使用总结--下拉列表和文件夹对话框属性

    上一篇有点短了,因为实在没有什么可说的,这一篇会多一点。O(_)O~

    一、下拉列表

    关于如何在CodeSmith中创建一个下拉列表的属性框其实很简单,是要使用C#中的枚举就行了,看操作。

    首先定义一个枚举,跟定义方法一样,也要包含在<script runat="template">……</script>标签中,代码语法就是我们在定义模板的时候选择的语法,这里我用的是C#,如下:

    <script runat="template">

    /// <summary>

    /// 定义一个下拉列表的枚举

    /// </summary>

    public enum CollectionTypeEnum 

    {

    One

    Two

    Three,

    Four,

    Five

    }

    </script>

    Like This,我们就定义好了一个枚举了。接下来就是我们在定义属性的时候,类型选择这个枚举。

    <%@ Property Name="DropDownList" Type="CollectionTypeEnum" Default="One" Description="这是一个下拉列表框"%>

    编译一下就能看见效果了。

     

    调用的时候就像普通属性一样就行<%=DropDownList %>

    二、文件夹对话框

    好了,定义好了下拉列表框后我们就开始下一个内容,文件夹对话框。

    文件夹对话框有两种定义方式,一种是普通定义的那种,另一种就是用代码定义,不过我经常用第一种,谁让他简单呢。

    首先我们要引用两个命名空间System.Windows.Forms.DesignSystem.Drawing.Design如下:

    <%@ Import Namespace="System.Windows.Forms.Design" %>

    <%@ Import Namespace="System.Drawing.Design" %>

    接下来只要在定义属性的时候,类型选择FolderNameEditor就行了

    <%@ Property Name="Test1" Type="String" Editor="FolderNameEditor" EditorBase="UITypeEditor" Optional="False" %>

    当然你也可以写全了,全路径如下:

    <%@ Property Name="Test2" Type="String" Editor="System.Windows.Forms.Design.FileNameEditor" EditorBase="System.Drawing.Design.UITypeEditor" Optional="False" %>

    编译一下就能看见了

     

    第二种方法也要引用命名空间,然后不添加属性标签,直接在代码中写出来。具体代码如下:

    #region 定义一个弹出选择文件夹获取路径的属性
    private string _outputDirectory = String.Empty;
    /// <summary>
    /// 调用文件夹对话框
    /// </summary>
    [Editor(typeof(System.Windows.Forms.Design.FolderNameEditor), typeof(System.Drawing.Design.UITypeEditor))] 
    [Optional, NotChecked]
    [DefaultValue("")]
    /// <summary>
    /// 属性名
    /// </summary>
    public string OutputDirectory
    { 
        get
        {
            return _outputDirectory;
        }
        set
        {
            if (value.EndsWith("\")) value = value.Substring(0, value.Length -1);
            _outputDirectory = value;
        } 
    }
    #endregion

    用这种方法的好处是如果我们要处理路径,在输出前处理就可以了,不用再单另的写代码了。呵呵

    这里有一点FolderNameEditor是文件夹,那么相应的FileNameEditor就是文件了。

    取值方法如同正常属性一样。

    示例模板:

    <%-- 

    Name: 测试模板4

    Author: GodFinal

    Description: 

    --%>

    <%@ Template Language="C#" TargetLanguage="C#" Description="" %>

     

    <%@ Property Name="DropDownList" Type="CollectionTypeEnum" Default="One" Description="这是一个下拉列表框"%>

     

    <%@ Import Namespace="System.Windows.Forms.Design" %>

    <%@ Import Namespace="System.Drawing.Design" %>

     

    <%@ Property Name="OutputDirectory1" Type="String" Editor="FolderNameEditor" EditorBase="UITypeEditor" Optional="False" %>

     

    <%@ Property Name="OutputDirectory2" Type="String" Editor="System.Windows.Forms.Design.FileNameEditor" EditorBase="System.Drawing.Design.UITypeEditor" Optional="False" %>

     

    <%=DropDownList %>

     

    <%=OutputDirectory%>

     

    <%=OutputDirectory1%>

     

    <%=OutputDirectory2%>

    <script runat="template">

    /// <summary>

    /// 定义一个下拉列表的枚举

    /// </summary>

    public enum CollectionTypeEnum 

    {

        One, 

      Two, 

      Three,

      Four,

      Five

    }

     

    #region 定义一个弹出选择文件夹对话框获取路劲的属性

    private string _outputDirectory = String.Empty;

    /// <summary>

    /// 调用文件夹对话框

    /// </summary>

    [Editor(typeof(System.Windows.Forms.Design.FolderNameEditor), typeof(System.Drawing.Design.UITypeEditor))] 

    [Optional, NotChecked]

    [DefaultValue("")]

    /// <summary>

    /// 属性名

    /// </summary>

    public string OutputDirectory

        get

        {

            return _outputDirectory;

        }

        set

        {

            if (value.EndsWith("\")) value = value.Substring(0, value.Length -1);

            _outputDirectory = value;

        } 

    }

    #endregion

    </script>

    最终结果:

     

     

  • 相关阅读:
    最优装载问题---贪心算法
    windows 10 资源管理器多余盘符去除
    js版本排序改造vue版本
    async and await
    echarts问题
    vue兼容到IE9
    expdp/impdp 参数说明,中英对照
    MySQL ibdata1文件迁移
    Oracle AWR报告提取方法
    Vertica DBD 分析优化设计
  • 原文地址:https://www.cnblogs.com/GodFinal/p/3512464.html
Copyright © 2011-2022 走看看