C# 在不实现ICommand的情况下创建RelayCommand

C# 在不实现ICommand的情况下创建RelayCommand,c#,mvvm,relaycommand,C#,Mvvm,Relaycommand,我的问题是如何创建一个行为与RelayCommand相同但不为MVVM应用程序实现ICommand的类?任何建议都将不胜感激 我的RelayCommand[实现ICommand]如下所示: public class RelayCommand:ICommand { private readonly Action<object> m_Execute; private readonly Predicate<object> m_CanEx

我的问题是如何创建一个行为与RelayCommand相同但不为MVVM应用程序实现ICommand的类?任何建议都将不胜感激

我的RelayCommand[实现ICommand]如下所示:

 public class RelayCommand:ICommand
    {
        private readonly Action<object> m_Execute;
        private readonly Predicate<object> m_CanExecute;

        public RelayCommand(Action<object> exec) : this(exec, null) { }
        public RelayCommand(Action<object> exec, Predicate<object> canExec)
        {
            if (exec == null)
                throw new ArgumentNullException("exec");

            m_Execute = exec;
            m_CanExecute = canExec;
        }

        #region ICommand Members

        public bool CanExecute(object parameter)
        {
            if (parameter == null)
                return true;
            else
                return m_CanExecute(parameter);
        }

        public event EventHandler CanExecuteChanged
        {
            add
            {
                if (m_CanExecute != null)
                    CommandManager.RequerySuggested += value;
            }
            remove
            {
                if (m_CanExecute != null)
                    CommandManager.RequerySuggested -= value;
            }
        }

        public void Execute(object parameter)
        {
            m_Execute(parameter);
        }

        #endregion
    }
公共类RelayCommand:ICommand
{
私有只读操作m_Execute;
私有只读谓词m_CanExecute;
公共RelayCommand(Action exec):此(exec,null){}
公共RelayCommand(Action exec,谓词canExec)
{
if(exec==null)
抛出新的ArgumentNullException(“exec”);
m_Execute=exec;
m_CanExecute=canExec;
}
#区域ICommand成员
公共布尔CanExecute(对象参数)
{
if(参数==null)
返回true;
其他的
返回m_CanExecute(参数);
}
公共事件事件处理程序CanExecuteChanged
{
添加
{
如果(m_CanExecute!=null)
CommandManager.RequerySuggested+=值;
}
去除
{
如果(m_CanExecute!=null)
CommandManager.RequerySuggested-=值;
}
}
public void Execute(对象参数)
{
m_Execute(参数);
}
#端区
}

你这样做的动机是什么?我没有动机。在我的一次采访中,当我向他展示我上面的RelayCommand实现时,我被要求实现它。所以,这让我很烦恼。只要从代码中删除
:ICommand
,它就不会再实现它了。。。“但这是毫无意义的。”纪尧姆:对不起。我的RelayCommand是独立的,但我的ViewModel正在使用它。如果我删除ICommand,我的代码将不会被编译。我想编译我的应用程序,但不需要ICommand就可以轻松地执行command。也许可以与面试官跟进他们的意思和想要实现的目标——在面试结束后问一个关于面试的问题可能是一个积极的迹象。