【问题标题】:The event 'Command.CanExecuteChanged' can only appear on the left hand side of += or -=事件 'Command.CanExecuteChanged' 只能出现在 += 或 -= 的左侧
【发布时间】:2018-08-10 06:21:24
【问题描述】:

我的 Command-ViewModel 中是否缺少某些内容?

 public class Command : ICommand
    {
        public Command(Action execute, Func<bool> canExecute = null)
        {
            this.execute = execute;
            this.canExecute = canExecute ?? new Func<bool>(() => true);
        }
        private Action execute;
        private Func<bool> canExecute;
        public event EventHandler CanExecuteChanged;

        public bool CanExecute(object parameter)
        {
            return canExecute.Invoke();
        }

        public void Execute(object parameter)
        {
            execute?.Invoke();
        }
    }

每次我想在我的MainViewModel 中使用CanExecuteChanged 和这行代码((Command)MyCommand).CanExecuteChanged(); 它都会给我这个错误The event 'Command.CanExecuteChanged' can only appear on the left hand side of += or -=

【问题讨论】:

    标签: c# xamarin.forms


    【解决方案1】:

    CanExecuteChanged 是一个事件。你只能这样使用它:

    YourCommand.CanExecuteChanged += (sender, args) =>
    {
        // Do your magic here
    };
    

    【讨论】:

      【解决方案2】:
      public event EventHandler CanExecuteChanged;
      

      是语法糖。当你把它放进去时编译器实际生成的是*

      private EventHandler _CanExecuteChanged;
      public event EventHandler CanExecuteChanged
      {
          add { _CanExecuteChanged += value; }
          remove { _CanExecuteChanged -= value; }
      }
      

      因此,公开暴露的 CanExecuteChange 不是实际字段,而只是您可以添加和删除处理程序的内容。

      相关说明:支持字段为private 也是在基类中具有protected OnXXXX() 方法的正常模式的原因。

      public event EventHandler CanExecuteChanged;
      protected void OnCanExecuteChanged(EventArgs args)
      {
          CanExecuteChanged?.Invoke(this, args);
      }
      

      *注意“喜欢”部分;正确添加和删除也需要一些空值检查。

      【讨论】:

        【解决方案3】:

        要回答您的问题,是的,您的代码中缺少某些内容。我不知道你是否在使用 Xamarin.Forms 提供的 Command 类,但如果你不是,那么你真的应该使用!

        除了订阅事件通知之外,您最终无法与它所属的类之外的event 进行交互,这就是“事件 'Command.CanExecuteChanged' 只能出现在 += 的左侧或 -=' 告诉你。要订阅,您需要执行以下操作:

        MyCommand.CanExecuteChanged += (sender, e) =>
        {
            // Your code to react to the event goes here.
        };
        

        你可以做什么,这就是你需要使用 Xamarin.Forms Command 类的地方(或者你可以在你的 Command 类中自己实现类似的东西)。是电话ChangeCanExecute 例如

        ((Command)MyCommand).ChangeCanExecute();
        

        这将触发事件被触发,从而更新绑定到该命令的任何 UI 控件。

        【讨论】:

        • 我现在使用 Xamarin.Forms... 和 ((Command)MyCommand).ChangeCanExecute(); 使它变得如此简单。现在它工作正常。谢谢。
        【解决方案4】:

        我删除了我的Command-Class,现在我使用了Xamarin.Forms Command Class,这让这件事变得容易多了,因为现在我可以使用这段美味的短代码:((Command)MyCommand).ChangeCanExecute(); 来触发事件。

        谢谢大家。

        【讨论】:

          【解决方案5】:

          只是另一种方法!

          public sealed class Command : ICommand
          {
              private readonly Predicate<object> _canExecute;
              private readonly Action<object> _execute;
          
              public event EventHandler CanExecuteChanged
              {
                  add => CommandManager.RequerySuggested += value;
                  remove => CommandManager.RequerySuggested -= value;
              }
          
              public RelayCommand(Predicate<object> CanExecute, Action<object> Execute)
              {
                  _canExecute = CanExecute;
                  _execute = Execute;
              }
          
              public bool CanExecute(object parameter)
              {
                  return _canExecute(parameter);
              }
          
              public void Execute(object parameter)
              {
                  _execute(parameter);
              }
          }
          

          然后让命令管理器处理更新您的.CanExecuteChanged 问题:)

          如果您需要强制命令管理器“再次查看”,请致电 CommandManager.InvalidateRequerySuggested();

          这里是关于命令管理器的更多信息 How does CommandManager.RequerySuggested work?

          【讨论】:

            猜你喜欢
            • 2020-08-16
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2021-08-02
            • 2020-01-10
            • 2016-12-13
            • 2013-02-08
            • 1970-01-01
            相关资源
            最近更新 更多