public class CommandBase : ICommand
{
public event EventHandler CanExecuteChanged;
public bool CanExecute(object parameter)
{
return DoCanExecute.Invoke(parameter);
}
public void Execute(object parameter)
{
DoExecute?.Invoke(parameter);
}
public Action
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
在ViewModel中定义事件,并初始化
public class SongPlayListViewModel
{
//定义命令
public CommandBase PlaySongClickCommand { get; set; }
public SongPlayListViewModel()
{
//初始化命令
PlaySongClickCommand = new CommandBase();
PlaySongClickCommand.DoExecute = new Action((o) =>
{
//命令触发逻辑
//.....
});
//按钮是否是在启用状态
PlaySongClickCommand.DoCanExecute = new Func((o) => { return true; });
}
}