MVVM Light Toolkit VM 用の Mvvm Property,Command の T4 テンプレート を公開します。
たったこれだけコーディングするだけで Mvvm Property,Command が出来上がります。
もうめんどくさいとか実装が大変だとか言わないでください。
Base クラスを作ってしまった。
なんか違う気がするが後悔はしていない。
MainViewModel.tt ソース
<#@ template debug="true" hostspecific="false" language="C#" #>
<#@ output extension=".properties.cs" #>
<#@ include file="../Template/MvvmClassInfo.ttinclude" #>
<#
var classinfo = new MvvmClassInfo(
"T4Mvvm.ViewModel", "MainViewModel",
new MvvmPropertyInfo[] {
new MvvmPropertyInfo("id","ID", typeof(int), ""),
new MvvmPropertyInfo("name","Name", typeof(string), "[Required()]")
},
new MvvmCommandInfo[] {
new MvvmCommandInfo("ShowMessageCommand", null),
new MvvmCommandInfo("ShowStringMessageCommand", typeof(string))
});
#>
<#@ include file="../Template/MvvmClassBody.ttinclude" #>
プロジェクトの構成は前回と一緒。
MvvmClassInfo.ttinclude ソース
<#+
class MvvmClassInfo
{
public string NameSpaceName;
public string ClassName;
public MvvmPropertyInfo[] Property;
public MvvmCommandInfo[] Command;
public MvvmClassInfo(string nameSpaceName, string className, MvvmPropertyInfo[] property, MvvmCommandInfo[] command)
{
NameSpaceName = nameSpaceName;
ClassName = className;
Property = property;
Command = command;
}
}
class MvvmPropertyInfo
{
public string Attribute;
public string PrivateName;
public string PublicName;
public Type Type;
public MvvmPropertyInfo(string privateName, string publicName, Type type, string attribute)
{
PrivateName = privateName;
PublicName = publicName;
Type = type;
Attribute = attribute;
}
}
class MvvmCommandInfo
{
public string Name;
public Type Type;
public MvvmCommandInfo(string name, Type type)
{
Name = name;
Type = type;
}
}
#>
MvvmClassBody.ttinclud ソース
using System;
using System.ComponentModel;
using System.ComponentModel.DataAnnotations;
using GalaSoft.MvvmLight;
using GalaSoft.MvvmLight.Command;
namespace <#= classinfo.NameSpaceName #>
{
public partial class <#= classinfo.ClassName #>Base : ViewModelBase, INotifyPropertyChanging
{
<# if (classinfo.Command != null) { #>
public <#= classinfo.ClassName #>Base()
{
<# foreach(var command in classinfo.Command) {
if (command.Type != null) { #>
_<#= command.Name #> = new RelayCommand<<#= command.Type.ToString() #>>(On<#= command.Name #>Excec, On<#= command.Name #>CanExcec);
<# } else { #>
_<#= command.Name #> = new RelayCommand(On<#= command.Name #>Excec, On<#= command.Name #>CanExcec);
<# }
} #>
}
<# } #>
<# if (classinfo.Property != null) {
foreach(var property in classinfo.Property) { #>
private <#=property.Type.ToString()#> <#= property.PrivateName #>;
partial void On<#= property.PublicName #>Changing(<#=property.Type.ToString()#> value);
partial void On<#= property.PublicName #>Changed();
<#=property.Attribute#>
public <#=property.Type.ToString()#> <#=property.PublicName#>
{
get
{
return this.<#= property.PrivateName #>;
}
set
{
if (this.<#= property.PrivateName #> != value)
{
this.On<#= property.PublicName #>Changing(value);
this.RaisePropertyChanging("<#= property.PublicName #>");
this.<#= property.PrivateName #> = value;
this.RaisePropertyChanged("<#= property.PublicName #>");
this.On<#= property.PublicName #>Changed();
}
}
}
<# }
} #>
<# if (classinfo.Command != null) {
foreach(var command in classinfo.Command) {
string relaycommand;
if (command.Type != null) {
relaycommand = "RelayCommand<" + command.Type.ToString() + ">";
} else {
relaycommand = "RelayCommand";
} #>
private <#= relaycommand #> _<#= command.Name #>;
partial void On<#= command.Name #>Changing(<#= relaycommand #> value);
partial void On<#= command.Name #>Changed();
public <#= relaycommand #> <#= command.Name #>
{
get
{
return _<#= command.Name #>;
}
set
{
if (_<#= command.Name #> != value)
{
this.On<#= command.Name #>Changing(value);
this.RaisePropertyChanging("<#= command.Name #>");
_<#= command.Name #> = value;
this.RaisePropertyChanged("<#= command.Name #>");
this.On<#= command.Name #>Changed();
}
}
}
<# }
} #>
<# if (classinfo.Command != null) {
foreach(var command in classinfo.Command) {
if (command.Type != null) { #>
protected virtual void On<#= command.Name #>Excec(<#= command.Type.ToString() #> parameter) {}
protected virtual bool On<#= command.Name #>CanExcec(<#= command.Type.ToString() #> parameter) { return true; }
<# } else { #>
protected virtual void On<#= command.Name #>Excec() {}
protected virtual bool On<#= command.Name #>CanExcec() { return true; }
<# }
} #>
public event PropertyChangingEventHandler PropertyChanging;
protected virtual void RaisePropertyChanging(String propertyName)
{
if ((this.PropertyChanging != null))
{
this.PropertyChanging(this, new PropertyChangingEventArgs(propertyName));
}
}
public override void Cleanup()
{
base.Cleanup();
<# foreach(var command in classinfo.Command) { #>
_<#= command.Name #> = null;
<# }
} #>
}
}
}
結果出来上がる MainViewModel.properties.cs
using System;
using System.ComponentModel;
using System.ComponentModel.DataAnnotations;
using GalaSoft.MvvmLight;
using GalaSoft.MvvmLight.Command;
namespace T4Mvvm.ViewModel
{
public partial class MainViewModelBase : ViewModelBase, INotifyPropertyChanging
{
public MainViewModelBase()
{
_ShowMessageCommand = new RelayCommand(OnShowMessageCommandExcec, OnShowMessageCommandCanExcec);
_ShowStringMessageCommand = new RelayCommand<System.String>(OnShowStringMessageCommandExcec, OnShowStringMessageCommandCanExcec);
}
private System.Int32 id;
partial void OnIDChanging(System.Int32 value);
partial void OnIDChanged();
public System.Int32 ID
{
get
{
return this.id;
}
set
{
if (this.id != value)
{
this.OnIDChanging(value);
this.RaisePropertyChanging("ID");
this.id = value;
this.RaisePropertyChanged("ID");
this.OnIDChanged();
}
}
}
private System.String name;
partial void OnNameChanging(System.String value);
partial void OnNameChanged();
[Required()]
public System.String Name
{
get
{
return this.name;
}
set
{
if (this.name != value)
{
this.OnNameChanging(value);
this.RaisePropertyChanging("Name");
this.name = value;
this.RaisePropertyChanged("Name");
this.OnNameChanged();
}
}
}
private RelayCommand _ShowMessageCommand;
partial void OnShowMessageCommandChanging(RelayCommand value);
partial void OnShowMessageCommandChanged();
public RelayCommand ShowMessageCommand
{
get
{
return _ShowMessageCommand;
}
set
{
if (_ShowMessageCommand != value)
{
this.OnShowMessageCommandChanging(value);
this.RaisePropertyChanging("ShowMessageCommand");
_ShowMessageCommand = value;
this.RaisePropertyChanged("ShowMessageCommand");
this.OnShowMessageCommandChanged();
}
}
}
private RelayCommand<System.String> _ShowStringMessageCommand;
partial void OnShowStringMessageCommandChanging(RelayCommand<System.String> value);
partial void OnShowStringMessageCommandChanged();
public RelayCommand<System.String> ShowStringMessageCommand
{
get
{
return _ShowStringMessageCommand;
}
set
{
if (_ShowStringMessageCommand != value)
{
this.OnShowStringMessageCommandChanging(value);
this.RaisePropertyChanging("ShowStringMessageCommand");
_ShowStringMessageCommand = value;
this.RaisePropertyChanged("ShowStringMessageCommand");
this.OnShowStringMessageCommandChanged();
}
}
}
protected virtual void OnShowMessageCommandExcec() {}
protected virtual bool OnShowMessageCommandCanExcec() { return true; }
protected virtual void OnShowStringMessageCommandExcec(System.String parameter) {}
protected virtual bool OnShowStringMessageCommandCanExcec(System.String parameter) { return true; }
public event PropertyChangingEventHandler PropertyChanging;
protected virtual void RaisePropertyChanging(String propertyName)
{
if ((this.PropertyChanging != null))
{
this.PropertyChanging(this, new PropertyChangingEventArgs(propertyName));
}
}
public override void Cleanup()
{
base.Cleanup();
_ShowMessageCommand = null;
_ShowStringMessageCommand = null;
}
}
}
MainViewModel.cs はこうなります。
namespace T4Mvvm.ViewModel
{
public partial class MainViewModel : MainViewModelBase
{
public string Welcome
{
get
{
return "Welcome to MVVM Light";
}
}
public MainViewModel()
{
if (IsInDesignMode)
{
}
else
{
}
}
public override void Cleanup()
{
base.Cleanup();
}
}
}