简体   繁体   English

MVVM ICommand和委托

[英]MVVM ICommand and delegate

I read a MVVM tutorial and I got lost in the Commands part. 我阅读了一个MVVM教程 ,我迷失在命令部分。

using System; 
using System.Windows.Input;

namespace MVVMDemo { 

   public class MyICommand : ICommand { 
      Action _TargetExecuteMethod; 
      Func<bool> _TargetCanExecuteMethod;

      public MyICommand(Action executeMethod) {
         _TargetExecuteMethod = executeMethod; 
      }

      public MyICommand(Action executeMethod, Func<bool> canExecuteMethod){ 
         _TargetExecuteMethod = executeMethod;
         _TargetCanExecuteMethod = canExecuteMethod; 
      }

      public void RaiseCanExecuteChanged() { 
         CanExecuteChanged(this, EventArgs.Empty); 
      }

      bool ICommand.CanExecute(object parameter) { 

         if (_TargetCanExecuteMethod != null) { 
            return _TargetCanExecuteMethod(); 
         } 

         if (_TargetExecuteMethod != null) { 
            return true; 
         } 

         return false; 
      }

      // Beware - should use weak references if command instance lifetime 
         is longer than lifetime of UI objects that get hooked up to command 

      // Prism commands solve this in their implementation public event 
      EventHandler CanExecuteChanged = delegate { };

      void ICommand.Execute(object parameter) { 
         if (_TargetExecuteMethod != null) {
            _TargetExecuteMethod(); 
         } 
      } 
   } 
}

I'm not understanding the purpose of the RaiseCanExecuteChanged method and the line EventHandler CanExecuteChanged = delegate { }; 我不了解RaiseCanExecuteChanged方法和EventHandler CanExecuteChanged = delegate { };行的目的EventHandler CanExecuteChanged = delegate { }; . I read that EventHandler is a delegate, but what kind of delegate is it? 我读到EventHandler是一个委托,但它是什么样的委托? What does the delegate { }; delegate { };是什么delegate { }; statement return? 声明回归?

Answering your second question, yes EventHandler is delegate of type void EventHandler(object sender, EventArgs args) . 回答你的第二个问题,是的, EventHandlervoid EventHandler(object sender, EventArgs args)类型的委托void EventHandler(object sender, EventArgs args) The following line is an default(Empty) anonymous delegate assigned to the EventHandler. 以下行是分配给EventHandler的默认(空)匿名委托。 May be to prevent NullReferenceException . 可能是为了防止NullReferenceException Below line can also be written as: 下面的行也可以写成:

EventHandler canExecute = delegate { };

To get the understanding of the usage of RaiseCanExecuteChanged read this answer here: https://stackoverflow.com/a/4531378/881798 要了解RaiseCanExecuteChanged的用法,请在此处阅读此答案: httpsRaiseCanExecuteChanged

Update - (by @Will) 更新 - (@Will)

RaiseCanExecuteChanged can be called by the code in the view model when the ability of the command to be executed has changed. 当执行命令的能力发生变化时,可以通过视图模型中的代码调用RaiseCanExecuteChanged。 For example, the save button returns false when UserName and Password are empty, but when they are filled, the VM calls RaiseCanExecuteChanged, and when the button asks the command if it can fire, it now responds in the positive. 例如,当UserName和Password为空时,保存按钮返回false,但是当它们被填满时,VM调用RaiseCanExecuteChanged,当按钮询问命令是否可以触发时,它现在响应为正。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM