项目使用MVVM,创建了一个基类VMBase
using System.ComponentModel;
using System.Runtime.CompilerServices;
namespace YKCore.ViewModels
{
public class VMBase : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
public void RaisePropertyChanged([CallerMemberName] string propertyName = "")
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
}
}
然后创建继承类的时候,要写一个属性,比较麻烦
private Visibility _BtnOKV;
public Visibility BtnOKV
{
get=>_BtnOKV;
set
{
_BtnOKV=value;
RaisePropertyChanged();
}
}
折腾了一会文本模板发现不错,比如下面的代码,就能自动生成一个类,效率还是蛮高的!
<#@ 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=".cs" #>
using System;
using System.Windows;
using System.Windows.Media;
namespace YKCore.ViewModels
{
public class VMMessageBox : VMBase
{
<#
var strs=@"string TBIconText,TBMsgText,TBOffsetText
Visibility ImageQRCodeV,BtnCancelV,BtnOKV
ImageSource ImageQRCodeSource
bool BtnOKIsDefault,BtnCancelIsDefault"
.Replace("\r\n","\r").Split('\r');
foreach(var str in strs)
{
var items=str.Split(' ');
foreach(var cname in items[1].Split(','))
{
Write(@$"
private {items[0]} _{cname};
public {items[0]} {cname}
{{
get=>_{cname};
set
{{
_{cname}=value;
RaisePropertyChanged();
}}
}}
");
}
}
#>
public VMCommand Commands{get;set;}
}
}
原文:https://www.cnblogs.com/catzhou/p/12487772.html