• MVVM模式的3种command总结[1]--DelegateCommand


    查了不少资料,大概理清楚的就是有3种。当然类名可以自己取了,不过为了便于记忆和区分,还是和看到的文章里面用一样的类名。

    1.DelegateCommand

    2.RelayCommand

    3.AttachbehaviorCommand

    因为MVVM模式适合于WPF和SL,所以这3种模式中也有一些小差异,比如RelayCommand下面的CommandManager方法就是WPF下面的,SL下面无法使用,不过我认为这3种方法中的基本思路都如出一辙,都是出自那位外国牛人的文章里面。主要的区别在于和VIEW中的控件的绑定使用上。有点不同的attachbehaviorcommand是prism4里面的一种设计模式,这个区别有点大。但我自己觉得最方便的还是这个DelegateCommand。

    复制代码
     1 /// <summary>
     2     /// Delegatecommand,这种WPF.SL都可以用,VIEW里面直接使用INTERACTION的trigger激发。比较靠谱,适合不同的UIElement控件
     3     /// </summary>
     4     public class DelegateCommand : ICommand
     5     {
     6         Func<object, bool> canExecute;
     7         Action<object> executeAction;
     8         bool canExecuteCache;
     9 
    10         public DelegateCommand(Action<object> executeAction, Func<object, bool> canExecute)
    11         {
    12             this.executeAction = executeAction;
    13             this.canExecute = canExecute;
    14         }
    15 
    16         #region ICommand Members
    17 
    18         public bool CanExecute(object parameter)
    19         {
    20             bool temp = canExecute(parameter);
    21 
    22             if (canExecuteCache != temp)
    23             {
    24                 canExecuteCache = temp;
    25                 if (CanExecuteChanged != null)
    26                 {
    27                     CanExecuteChanged(this, new EventArgs());
    28                 }
    29             }
    30 
    31             return canExecuteCache;
    32         }
    33 
    34         public event EventHandler CanExecuteChanged;
    35 
    36         public void Execute(object parameter)
    37         {
    38             executeAction(parameter);
    39         }
    40 
    41         #endregion
    42     }
    复制代码

    这个类大概可以这样来理解,构造函数中的action和func,action负责判断是否执行这个command,action就是触发这个command之后要执行的方法。这样理解最浅显,但对刚熟悉command的我来讲,这样最方便记忆和学习,为了使用ICommand接口实现的方法和事件的解释搜搜就可以找到,但是刚开始理解起来还是有点晦涩。

    下面是VM里面用这个command的例子。绑定了一个button控件,最简单例子。cm1Click就是构造函数里面的fuc,负责执行响应事件的方法。Cancm1Click就是构造函数里面的action,负责判断这个Command的响应事件是否执行,这里没有用到判断式,直接赋了一个true.

    复制代码
     1 public class TestViewModels:INotifyPropertyChanged
     2 {
     3         public TestViewModels()
     4         {
     5             ......
     6             cm1click = new DelegateCommand(cm1Click,Cancm1Click);   //初始化delegatecommand
     7             
     8         }
     9        ....
    10 
    11        //DelegateCommand
    12 
    13         #region command1
    14 
    15         public ICommand cm1click { get; set; }
    16         public void cm1Click(object param)
    17         {
    18             MessageBox.Show("CM1 clicked!");
    19         }
    20 
    21         private bool Cancm1Click(object param)
    22         {
    23             return true;
    24         }
    25 
    26         #endregion command1
    27        ......
    28 }
    复制代码

    在XAML里面,用interaction来绑定这个事件,而不是在button里面用command来绑定,这样做有个好处,就是非常直观,并且可以响应其他的很多事件

    复制代码
            <Button x:Name="BTN_CM1" Content="DelegateCommand" Height="115" Width="148" >
                <i:Interaction.Triggers>
                    <i:EventTrigger EventName="Click">
                        <i:InvokeCommandAction Command="{Binding cm1click}"/>
                    </i:EventTrigger>
                </i:Interaction.Triggers>
            </Button>
    复制代码

    如果是其他控件如grid等,要响应selectionchanged事件之类的,就可以直接在trigger里面把EventName修改下就可以了。在blend里面更方便,可以直接选择。但是有个问题一直没搞明白.这样的话,怎么获取事件的参数,我这里有个例子,再看看了再更新下。

  • 相关阅读:
    linux 删除某种规则命名的文件
    adb shell 出现 error :
    android 开发,多个线程共用一个handler
    android 开发上传图片遇到返回 FileNotFoundException
    mysql 的存储过程调试软件
    输入adb shell 时 提示error: more than one device and emulator
    高德开发 android 出现 key 鉴权失败
    android 中设置HttpURLConnection 超时并判断是否超时
    LINQ to SQL语句(3)之Count/Sum/Min/Max/Avg
    C# 如何判断数据是否为 NaN
  • 原文地址:https://www.cnblogs.com/changbaishan/p/3590598.html
Copyright © 2020-2023  润新知