MVVM模式的3種command總結[1]--DelegateCommand

查了很多資料,大概理清楚的就是有3種。固然類名能夠本身取了,不過爲了便於記憶和區分,仍是和看到的文章裏面用同樣的類名。html

1.DelegateCommand設計模式

2.RelayCommand函數

3.AttachbehaviorCommandpost

由於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接口實現的方法和事件的解釋搜搜就能夠找到,可是剛開始理解起來仍是有點晦澀。this

下面是VM裏面用這個command的例子。綁定了一個button控件,最簡單例子。cm1Click就是構造函數裏面的fuc,負責執行響應事件的方法。Cancm1Click就是構造函數裏面的action,負責判斷這個Command的響應事件是否執行,這裏沒有用到判斷式,直接賦了一個true.url

複製代碼
 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來綁定,這樣作有個好處,就是很是直觀,而且能夠響應其餘的不少事件spa

複製代碼
        <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裏面更方便,能夠直接選擇。可是有個問題一直沒搞明白.這樣的話,怎麼獲取事件的參數,我這裏有個例子,再看看了再更新下。設計

相關文章
相關標籤/搜索