背景:PropertyChanged和Command总是没有记住怎么写
PropertyChanged:
public event PropertyChangedEventHandler? PropertyChanged;
protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
-- 使用上特性[CallerMemberName]的作用就是由原本调用这个方法的使用要传入属性的名称作为这个方法的参数,但是现在就不用了,直接调用方法,会自动将参数(这里是属性的名称)传过去。所以调用就直接OnPropertyChanged()就行了,不需要原理的nameof(属性名)
补充:也可以加多一层使用表达式树做形参
public void RaisePropertyChanged<T>(Expression<Func<T>> expression)
{
MemberExpression member = (MemberExpression)expression.Body;
string propName = member.Member.Name;
RaisePropertyChanged(propName);
}
RelayCommand:
public class RelayCommand : ICommand
{
// 不需要怎么访问,就私有起来就行
private readonly Action<object> _execute;
private readonly Predicate<object> _canExecute; // 这个就是Func<object, bool>
public RelayCommand(Action<object> execute, Predicate<object> canExecute)
{
_execute = execute ?? throw new ArgumentNullException(nameof(execute)); // 一定要有这个值
_canExecute = canExecute;
}
// 自动调用CanExecute
public event EventHandler? CanExecuteChanged
{
add { CommandManager.RequerySuggested += value; }
remove { CommandManager.RequerySuggested -= value; }
}
// 手动调用CanExecute
public void RaiseCanExecuteChanged()
{
CommandManager.InvalidateRequerySuggested();
}
public bool CanExecute(object? parameter)
{
//Console.WriteLine(".....");
return _canExecute?.Invoke(parameter) ?? true; // 有就调用,没有就返回true
}
public void Execute(object? parameter)
{
//Console.WriteLine("激活方法");
//Debug.WriteLine("激活方法`11");
_execute?.Invoke(parameter); // 调用订阅的方法
}
}
泛型版本:
public class RelayCommand<T> : ICommand
{
private readonly Action<T> _execute;
private readonly Func<T, bool> _canExecute;
// 构造方法订阅
public RelayCommand(Action<T> execute, Func<T, bool> canExecute)
{
_execute = execute ??throw new ArgumentNullException(nameof(execute));
_canExecute = canExecute;
}
// 自动canExecute
public event EventHandler? CanExecuteChanged
{
add { CommandManager.RequerySuggested += value; }
remove { CommandManager.RequerySuggested -= value; }
}
// 手动调用onExecute
public void RaiseCanExecuteChanged()
{
CommandManager.InvalidateRequerySuggested();
}
public bool CanExecute(object? parameter)
{
return _canExecute?.Invoke((T)parameter) ?? true;
}
public void Execute(object? parameter)
{
_execute?.Invoke((T)parameter);
}
}
手写好麻烦还是用MvvmToolkit吧:MvvmToolkit的使用_mvvmtoolkit cs0117-CSDN博客