日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程语言 > asp.net >内容正文

asp.net

【WPF】MVVM模式的3种command

發(fā)布時(shí)間:2025/4/16 asp.net 35 豆豆
生活随笔 收集整理的這篇文章主要介紹了 【WPF】MVVM模式的3种command 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
原文:【W(wǎng)PF】MVVM模式的3種command

1.DelegateCommand

2.RelayCommand

3.AttachbehaviorCommand

因?yàn)镸VVM模式適合于WPF和SL,所以這3種模式中也有一些小差異,比如RelayCommand下面的CommandManager方法就是WPF下面的,SL下面無法使用,不過我認(rèn)為這3種方法中的基本思路都如出一轍,都是出自那位外國牛人的文章里面。主要的區(qū)別在于和VIEW中的控件的綁定使用上。有點(diǎn)不同的attachbehaviorcommand是prism4里面的一種設(shè)計(jì)模式,這個(gè)區(qū)別有點(diǎn)大。但我自己覺得最方便的還是這個(gè)DelegateCommand。

DelegateCommand

/// <summary>/// Delegatecommand,這種WPF.SL都可以用,VIEW里面直接使用INTERACTION的trigger激發(fā)。比較靠譜,適合不同的UIElement控件/// </summary>public class DelegateCommand : ICommand{Func<object, bool> canExecute;Action<object> executeAction;bool canExecuteCache;public DelegateCommand(Action<object> executeAction, Func<object, bool> canExecute){this.executeAction = executeAction;this.canExecute = canExecute;}#region ICommand Memberspublic bool CanExecute(object parameter){bool temp = canExecute(parameter);if (canExecuteCache != temp){canExecuteCache = temp;if (CanExecuteChanged != null){CanExecuteChanged(this, new EventArgs());}}return canExecuteCache;}public event EventHandler CanExecuteChanged;public void Execute(object parameter){executeAction(parameter);}#endregion}

這個(gè)類大概可以這樣來理解,構(gòu)造函數(shù)中的action和func,action負(fù)責(zé)判斷是否執(zhí)行這個(gè)command,action就是觸發(fā)這個(gè)command之后要執(zhí)行的方法。這樣理解最淺顯,但對(duì)剛熟悉command的我來講,這樣最方便記憶和學(xué)習(xí),為了使用ICommand接口實(shí)現(xiàn)的方法和事件的解釋搜搜就可以找到,但是剛開始理解起來還是有點(diǎn)晦澀。

下面是VM里面用這個(gè)command的例子。綁定了一個(gè)button控件,最簡(jiǎn)單例子。cm1Click就是構(gòu)造函數(shù)里面的fuc,負(fù)責(zé)執(zhí)行響應(yīng)事件的方法。Cancm1Click就是構(gòu)造函數(shù)里面的action,負(fù)責(zé)判斷這個(gè)Command的響應(yīng)事件是否執(zhí)行,這里沒有用到判斷式,直接賦了一個(gè)true.

public class TestViewModels:INotifyPropertyChanged {public TestViewModels(){......cm1click = new DelegateCommand(cm1Click,Cancm1Click); //初始化delegatecommand}....//DelegateCommand#region command1public ICommand cm1click { get; set; }public void cm1Click(object param){MessageBox.Show("CM1 clicked!");}private bool Cancm1Click(object param){return true;}#endregion command1...... }

在XAML里面,用interaction來綁定這個(gè)事件,而不是在button里面用command來綁定,這樣做有個(gè)好處,就是非常直觀,并且可以響應(yīng)其他的很多事件

<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>

RelayCommand

RelayCommand本來是WPF下面用的一種自定義的command,主要是它用到了事件管理函數(shù),這個(gè)SL下面是沒有的。不過這部分代碼如果修改一下,也可以在SL下面使用,和WPF下面的實(shí)現(xiàn)思路差不多。

先看下RelayCommand的定義,一共有2種。

public class RelayCommand<T> : ICommand{public RelayCommand(Action<T> execute): this(execute, null){}public RelayCommand(Action<T> execute, Predicate<T> canExecute){if (execute == null)throw new ArgumentNullException("execute");_execute = execute;_canExecute = canExecute;}[DebuggerStepThrough]public bool CanExecute(object parameter){return _canExecute == null ? true : _canExecute((T)parameter);}public event EventHandler CanExecuteChanged{add{}remove{} //add//{// if (_canExecute != null)// CommandManager.RequerySuggested += value;//}//remove//{// if (_canExecute != null)// CommandManager.RequerySuggested -= value;//}}public void Execute(object parameter){_execute((T)parameter);}readonly Action<T> _execute = null;readonly Predicate<T> _canExecute = null;bool ICommand.CanExecute(object parameter){throw new NotImplementedException();}event EventHandler ICommand.CanExecuteChanged{add { throw new NotImplementedException(); }remove { throw new NotImplementedException(); }}void ICommand.Execute(object parameter){throw new NotImplementedException();}}

第一種是采用泛型的Relaycommand定義

public class RelayCommand : ICommand{public RelayCommand(Action execute): this(execute, null){}public RelayCommand(Action execute, Func<bool> canExecute){if (execute == null)throw new ArgumentNullException("execute");_execute = execute;_canExecute = canExecute;}[DebuggerStepThrough]public bool CanExecute(object parameter){return _canExecute == null ? true : _canExecute();}public event EventHandler CanExecuteChanged{ //這里把實(shí)現(xiàn)注釋掉了,這樣在SL下面也可以用。add { }remove { }//add//{// if (_canExecute != null)// CommandManager.RequerySuggested += value;//}//remove//{// if (_canExecute != null)// CommandManager.RequerySuggested -= value;//}}public void Execute(object parameter){_execute();}readonly Action _execute;readonly Func<bool> _canExecute;}

第二種就是最常用的定義,可以看到在CanExecuteChanged事件里面把commmandmanager方法給注釋掉了,就可以在SL下面使用這個(gè)類,而且現(xiàn)在看好像也沒有什么問題。

在代碼上看,Relaycommand和delegatcommand基本上沒有啥區(qū)別,也是實(shí)現(xiàn)了func和action兩個(gè)參數(shù)的辦法,基本思路一樣。

它們最大的區(qū)別就是在前端的調(diào)用方式上。delegatecommand使用了expression的SDK里面的interaction來綁定事件,而這種就是直接通過buttonbase的command屬性來綁定,因此只能執(zhí)行單擊事件,所以使用范圍比較局限,不過如果用interaction來綁定事件的話,其實(shí)實(shí)現(xiàn)就和delegatecommand一樣了。不過為了總結(jié)下學(xué)習(xí),還是分開來區(qū)別下。

前端XAML的代碼

<Button x:Name="BTN_CM2" Content="Command2" Height="103" HorizontalAlignment="Left" Margin="115,123,0,0" VerticalAlignment="Top" Width="109" Command="{Binding command2}" />

后臺(tái)

private ICommand _command2;public ICommand command2{get{if (this._command2 == null){this._command2 = new RelayCommand(() => this.cm2Click(),() => this.Cancm2Click);}return this._command2;}set { }}public bool Cancm2Click{get { return true; }}public void cm2Click(){MessageBox.Show("CM2 Clicked!");}

總結(jié)

以上是生活随笔為你收集整理的【WPF】MVVM模式的3种command的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。