.Net T4 模板 執行個體(一)

來源:互聯網
上載者:User

標籤:c#   wpf   mvvm   t4   

T4模板基礎就不在這裡重複了,園子裡有很多文章講解T4模板。

今天給大家介紹一個執行個體 主要是針對WPF MVVM中Model的書寫,

Model的格式大致是:

    // 測試    private string a;    /// <summary>    /// 測試    /// <summary>    public string A    {        get         {             return a;        }        set        {            if (a != value)            {                a = value;                this.RaisePropertyChanged("A");            }        }    }

在Model 類中一般都是重複性質的工作,所有選擇T4來產生該檔案是個不錯的選擇。

產生model的核心代碼如下:

BaseModelT4.tt

<#@ template language="C#" #><#@ assembly name="System.Core" #><#@ import namespace="System.Linq" #><#@ import namespace="System.Text" #><#@ import namespace="MVVMT4.models" #><#@ import namespace="System.Collections.Generic" #><#@ include file="BaseT4.tt" #><#foreach(ModelT4Model item in ModelT4OC) {#>    // <#=item.Notes#>    private <#=item.TypeName#> <#=Lower(item.PropertyName) #>;    /// <summary>    /// <#=item.Notes#>    /// <summary>    public <#=item.TypeName #> <#=Uppercase(item.PropertyName) #>    {        get         {             return <#=Lower(item.PropertyName) #>;        }        set        {            if (<#=Lower(item.PropertyName) #> != value)            {                <#=Lower(item.PropertyName) #> = value;                this.RaisePropertyChanged("<#=Uppercase(item.PropertyName) #>");            }        }    }<#}#>

BaseModelT4_P.cs

public partial class BaseModelT4    {        #region 欄位        private ObservableCollection<ModelT4Model> _ModelT4OC = new ObservableCollection<ModelT4Model>();        #endregion        #region 屬性        public ObservableCollection<ModelT4Model> ModelT4OC        {            get { return _ModelT4OC; }            set { _ModelT4OC = value; }        }        #endregion    }

BaseT4.tt

<#@ template debug="false" hostspecific="false" language="C#" #><#@ assembly name="System.Core" #><#@ import namespace="System.Linq" #><#@ import namespace="System.Text" #><#@ import namespace="System.Collections.Generic" #><#@ output extension=".txt" #><#+public string Uppercase(string s)    {        if(string.IsNullOrEmpty(s))        {            return string.Empty;        }        string notConvertS = s.Substring(1);        string convertS=s.Substring(0,1);        convertS = convertS.ToUpperInvariant();        return convertS + notConvertS;    }    public string Lower(string s)    {        if(string.IsNullOrEmpty(s))        {            return string.Empty;        }        string notConvertS = s.Substring(1);        string convertS=s.Substring(0,1);        convertS = convertS.ToLowerInvariant();        return convertS + notConvertS;    }#>
ModelT4Model.cs

public class ModelT4Model : INotifyPropertyChanged    {        #region 欄位        private string _TypeName = string.Empty;        private string _PropertyName = string.Empty;        //注釋        private string _Notes = string.Empty;        #endregion        #region 屬性        public string TypeName        {            get { return _TypeName; }            set            {                if (_TypeName != value)                {                    _TypeName = value;                    OnPropertyChanged("TypeName");                }            }        }        public string PropertyName        {            get { return _PropertyName; }            set            {                if (_PropertyName != value)                {                    _PropertyName = value;                    OnPropertyChanged("PropertyName");                }            }        }        /// <summary>        /// 注釋        /// </summary>        public string Notes        {            get { return _Notes; }            set            {                if (_Notes != value)                {                    _Notes = value;                    OnPropertyChanged("Notes");                }            }        }        #endregion        #region 方法        private void OnPropertyChanged(string propertyName)        {            if (PropertyChanged != null)            {                PropertyChanged(this, new PropertyChangedEventArgs(propertyName));            }        }        #endregion        #region 事件        public event PropertyChangedEventHandler PropertyChanged;        #endregion    }


源碼下載

.Net T4 模板 執行個體(一)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.