今天學(xué)了MVVM模式,這里貼一下具體步驟。
MVVM其實(shí)就是:Model 、view、viewmodel三個(gè)的簡(jiǎn)稱,就像MVC一樣。
model就是模型。view就是視圖。viewmodel就是和view進(jìn)行綁定的。
首先建立一個(gè)MainWindow。
?然后建立相應(yīng)的文件夾:一個(gè)ViewModels,一個(gè)views,一個(gè)Models。在viewmodels里面有一個(gè)基礎(chǔ)的通知類:NotifycationObject,這個(gè)類繼承自INotifyPropertyChanged,然后實(shí)現(xiàn)它就可以了。這個(gè)類是通知的基礎(chǔ)類,當(dāng)事件執(zhí)行時(shí),會(huì)將相應(yīng)的值顯示在綁定的控件上。
///<summary>/// 通知基類。如果RaisePropertyChanged被調(diào)用,databinding就會(huì)調(diào)用PropertyChanged事件,將propertyName的值顯示在界面
?///</summary>class NotifycationObject : INotifyPropertyChanged
? ? {
? ? ? ? publicevent PropertyChangedEventHandler PropertyChanged;
? ? ? ? publicvoidRaisePropertyChanged(string propertyName)
? ? ? ? {
? ? ? ? ? ? if(PropertyChanged!=null)
? ? ? ? ? ? {
? ? ? ? ? ? ? ? PropertyChanged.Invoke(this,new PropertyChangedEventArgs(propertyName));
? ? ? ? ? ? }
? ? ? ? }
? ? }
然后我們需要建立一個(gè)Commands文件夾,里面有一個(gè)基礎(chǔ)的委托命令類:DelegateCommand。
class DelegateCommand : ICommand
? ? {
? ? ? ? publicevent EventHandler CanExecuteChanged;
? ? ? ? publicboolCanExecute(object parameter)
? ? ? ? {
? ? ? ? ? ? if(CanExecuteFunc ==null)
? ? ? ? ? ? {
? ? ? ? ? ? ? ? returntrue;
? ? ? ? ? ? }
? ? ? ? ? ? returnthis.CanExecuteFunc(parameter);
? ? ? ? }
? ? ? ? publicvoidExecute(object parameter)
? ? ? ? {
? ? ? ? ? ? if(ExecuteAction ==null)
? ? ? ? ? ? {
? ? ? ? ? ? ? ? return;
? ? ? ? ? ? }
? ? ? ? ? ? this.ExecuteAction(parameter);
? ? ? ? }
? ? ? ? publicAction ExecuteAction {get;set; }
? ? ? ? publicFunc CanExecuteFunc {get;set; }
? ? }
然后我們需要在ViewModel里面建一個(gè)與Window相應(yīng)的ViewModel類。此類時(shí)需要綁定界面的控件的。viewmodel需要繼承
NotifycationObject
class MainWindowViewModel : NotifycationObject
? ? {
? ? ? ? privatedouble input1;
? ? ? ? publicdouble Input1
? ? ? ? {
? ? ? ? ? ? get{return input1; }
? ? ? ? ? ? set? ? ? ? ? ? {
? ? ? ? ? ? ? ? input1 = value;
? ? ? ? ? ? ? ? this.RaisePropertyChanged("Input1");
? ? ? ? ? ? }
? ? ? ? }
? ? ? ? privatedouble input2;
? ? ? ? publicdouble Input2
? ? ? ? {
? ? ? ? ? ? get{return input2; }
? ? ? ? ? ? set? ? ? ? ? ? {
? ? ? ? ? ? ? ? input2 = value;
? ? ? ? ? ? ? ? this.RaisePropertyChanged("Input2");
? ? ? ? ? ? }
? ? ? ? }
? ? ? ? privatedouble result;
? ? ? ? publicdouble Result
? ? ? ? {
? ? ? ? ? ? get{return result; }
? ? ? ? ? ? set? ? ? ? ? ? {
? ? ? ? ? ? ? ? result = value;
? ? ? ? ? ? ? ? this.RaisePropertyChanged("Result");
? ? ? ? ? ? }
? ? ? ? }
? ? ? ? publicDelegateCommand AddCommand {get;set; }
? ? ? ? ///<summary>/// 當(dāng)result被賦值之后,會(huì)調(diào)用屬性的set器。然后會(huì)調(diào)用RaisePropertyChanged的事件,而調(diào)用此事件之后,會(huì)將result顯示在界面上
? ? ? ? ///</summary>///<param name="obj"></param>privatevoidAdd(object obj)
? ? ? ? {
? ? ? ? ? ? this.Result = input1 + input2;
? ? ? ? }
? ? ? ? public MainWindowViewModel()
? ? ? ? {
? ? ? ? ? ? this.AddCommand =new DelegateCommand();
? ? ? ? ? ? this.AddCommand.ExecuteAction =newAction(Add);
? ? ? ? }
? ? }
其中:Input1、Input2、Result等3屬性是需要綁定在界面的三個(gè)textbox上的。而AddCommand是需要綁定在按鈕上的。AddCommand是需要傳遞給基礎(chǔ)委托命令類DelegateCommand的ExecuteAction的委托的,以此來綁定方法。
接下來就是綁定屬性到控件上了。
?如圖所示。
最后就是將當(dāng)前window的datacontext綁定viewmodel了,不然會(huì)找不到綁定的命令或者屬性。
?好了,一個(gè)簡(jiǎn)單的MVVM就完成了。
看看運(yùn)行的結(jié)果
這是初始運(yùn)行界面:只有默認(rèn)值
點(diǎn)擊OK后:
?。注意:我們這里是沒有給按鈕添加事件的,只給它了綁定的命令。這樣的好處就是當(dāng)界面變化,我們可以改動(dòng)最少的代碼。比如如果客戶讓你將textbox改成silder,你也不用再去添加事件啥的了,只需要改下綁定的屬性即可