You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
67 lines
1.7 KiB
67 lines
1.7 KiB
using System;
|
|
using System.Windows.Input;
|
|
|
|
namespace IOTContainer.MvvmBase
|
|
{
|
|
/// <summary>
|
|
/// 命令基础类
|
|
/// </summary>
|
|
public class CommandBase : ICommand
|
|
{
|
|
#region 字段属性
|
|
/// <summary>
|
|
/// 事件
|
|
/// </summary>
|
|
private Action<object> _action;
|
|
|
|
/// <summary>
|
|
/// 判断可执行方法
|
|
/// </summary>
|
|
private Predicate<object> _canExecuteFunc;
|
|
#endregion
|
|
|
|
#region 对外方法
|
|
/// <summary>
|
|
/// 初始化事件
|
|
/// </summary>
|
|
/// <param name="action"></param>
|
|
public CommandBase(Action<object> action)
|
|
{
|
|
this._action = action;
|
|
}
|
|
|
|
public CommandBase(Action<object> action, Predicate<object> canExecuteFunc)
|
|
{
|
|
this._action = action;
|
|
this._canExecuteFunc = canExecuteFunc;
|
|
}
|
|
|
|
/// <summary>
|
|
/// 执行事件
|
|
/// </summary>
|
|
/// <param name="parameter"></param>
|
|
public void Execute(object parameter)
|
|
{
|
|
this._action?.Invoke(parameter);
|
|
}
|
|
|
|
/// <summary>
|
|
/// 执行改变事件
|
|
/// </summary>
|
|
public event EventHandler CanExecuteChanged
|
|
{
|
|
add { CommandManager.RequerySuggested += value; }
|
|
remove { CommandManager.RequerySuggested -= value; }
|
|
}
|
|
/// <summary>
|
|
/// 执行改变
|
|
/// </summary>
|
|
/// <param name="parameter"></param>
|
|
/// <returns></returns>
|
|
public bool CanExecute(object parameter)
|
|
{
|
|
return this._canExecuteFunc?.Invoke(parameter) != false;
|
|
}
|
|
#endregion
|
|
}
|
|
}
|