MVVM模式代碼實例

Version:1.0StartHTML:000000209EndHTML:000041669StartFragment:000013042EndFragment:000041595StartSelection:000013064EndSelection:000041579SourceURL:https://www.cnblogs.com/bwlluck/p/7011029.html

一個簡單的WPF MVVM實例【轉(zhuǎn)載】

引用地址:http://blog.csdn.net/yl2isoft/article/details/20838149

1?新建WPF?應(yīng)用程序WPFMVVMExample

程序結(jié)構(gòu)如下圖所示。


2?Model實現(xiàn)

在Model文件夾下新建業(yè)務(wù)類StudentModel(類文件StudentModel.cs),類的詳細(xì)代碼如下所示。


[csharp]view plaincopy


using?System.ComponentModel;??


namespace?WPFMVVMExample.Model??

{??

publicclass?StudentModel?:?INotifyPropertyChanged??

????{??

///?<summary>??

///?學(xué)號??

///?</summary>??

privateint?studentId;??

publicint?StudentId??

????????{??

get???

????????????{???

return?studentId;???

????????????}??

set???

????????????{???

????????????????studentId?=?value;??

NotifyPropertyChanged("StudentId");??

????????????}??

????????}??


///?<summary>??

///?姓名??

///?</summary>??

privatestring?studentName;??

publicstring?StudentName??

????????{??

get???

????????????{???

return?studentName;???

????????????}??

set???

????????????{???

????????????????studentName?=?value;??

NotifyPropertyChanged("StudentName");??

????????????}??

????????}??


///?<summary>??

///?年齡??

///?</summary>??

privateint?studentAge;??

publicint?StudentAge??

????????{??

get???

????????????{???

return?studentAge;???

????????????}??

set???

????????????{???

????????????????studentAge?=?value;??

NotifyPropertyChanged("StudentAge");??

????????????}??

????????}??


///?<summary>??

///?Email??

///?</summary>??

privatestring?studentEmail;??

publicstring?StudentEmail??

????????{??

get???

????????????{???

return?studentEmail;???

????????????}??

set???

????????????{???

????????????????studentEmail?=?value;??

NotifyPropertyChanged("StudentEmail");??

????????????}??

????????}??


///?<summary>??

///?性別??

///?</summary>??

privatestring?studentSex;??

publicstring?StudentSex??

????????{??

get???

????????????{???

return?studentSex;???

????????????}??

set???

????????????{???

????????????????studentSex?=?value;??

NotifyPropertyChanged("StudentSex");??

????????????}??

????????}??


publicevent?PropertyChangedEventHandler?PropertyChanged;??

publicvoid?NotifyPropertyChanged(string?propertyName)??

????????{??

if?(PropertyChanged?!=null)??

????????????{??

PropertyChanged(this,new?PropertyChangedEventArgs(propertyName));??

????????????}??

????????}??

????}??

}??


StudentModel類實現(xiàn)了接口INotifyPropertyChanged。當(dāng)類實現(xiàn)該接口后,便可以向執(zhí)行綁定的客戶端發(fā)出某一屬性值已更改的通知。


3?ViewModel實現(xiàn)

在ViewModel文件夾下新建類文件StudentViewModel.cs,類文件的詳細(xì)代碼如下所示。


[csharp]view plaincopy


using?System;??

using?System.Windows.Input;??

using?WPFMVVMExample.Model;??


namespace?WPFMVVMExample.ViewModel??

{??

publicclass?StudentViewModel??

????{??

public?DelegateCommand?ShowCommand?{get;set;?}??

public?StudentModel?Student?{get;set;?}??

public?StudentViewModel()??

????????{??

Student?=new?StudentModel();??

ShowCommand=new?DelegateCommand();??

ShowCommand.ExecuteCommand?=new?Action

????????}??

privatevoid?ShowStudentData(object?obj)??

????????{??

????????????Student.StudentId?=?1;??

Student.StudentName?="tiana";??

????????????Student.StudentAge?=?20;??

Student.StudentEmail?="8644003248@qq.com";??

Student.StudentSex?="大帥哥";??

????????}??


????}??


publicclass?DelegateCommand?:?ICommand??

????{??

public?Action?ExecuteCommand?=null;??

public?Func?CanExecuteCommand?=null;??

publicevent?EventHandler?CanExecuteChanged;??


publicbool?CanExecute(object?parameter)??

????????{??

if?(CanExecuteCommand?!=null)??

????????????{??

returnthis.CanExecuteCommand(parameter);??

????????????}??

else??

????????????{??

returntrue;??

????????????}??

????????}??


publicvoid?Execute(object?parameter)??

????????{??

if?(this.ExecuteCommand?!=null)??

????????????{???

this.ExecuteCommand(parameter);???

????????????}??

????????}??


publicvoid?RaiseCanExecuteChanged()??

????????{??

if?(CanExecuteChanged?!=null)??

????????????{??

CanExecuteChanged(this,?EventArgs.Empty);??

????????????}??

????????}??

????}??

}??


代碼中,除了定義StudentViewModel類外,還定義了DelegateCommand類,該類實現(xiàn)了ICommand接口。

ICommand接口中的Execute()方法用于命令的執(zhí)行,CanExecute()方法用于指示當(dāng)前命令在目標(biāo)元素上是否可用,當(dāng)這種可用性發(fā)生改變時便會觸發(fā)接口中的CanExecuteChanged事件。

我們可以將實現(xiàn)了ICommand接口的命令DelegateCommand賦值給Button(命令源)的Command屬性(只有實現(xiàn)了ICommandSource接口的元素才擁有該屬性),這樣Button便與命令進(jìn)行了綁定。


4?MainWindow.xaml實現(xiàn)

MainWindow.xaml的界面如下圖所示。

MainWindow.xaml界面的xaml代碼如下所示。


[html]view plaincopy


xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"??

xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"??

Title="MainWindow"Height="350"Width="525">??


MainWindow.xaml的后端代碼如下所示。


[csharp]view plaincopy


using?System.Windows;??

using?WPFMVVMExample.ViewModel;??


namespace?WPFMVVMExample??

{??

///?<summary>??

///?MainWindow.xaml?的交互邏輯??

///?</summary>??

public?partialclass?MainWindow?:?Window??

????{??

public?MainWindow()??

????????{??

????????????InitializeComponent();??

this.DataContext?=new?StudentViewModel();??

????????}??

????}??

}??



5?運(yùn)行程序

運(yùn)行程序,點擊“顯示”按鈕,即將數(shù)據(jù)綁定至界面顯示。


6?說明

WPF中使用MVVM可以降低UI顯示與后端邏輯代碼的耦合度,即更換界面時,只需要修改很少的邏輯代碼就可以實現(xiàn),甚至不用修改。

在WinForm開發(fā)中,我們一般會直接操作界面的元素(如:TextBox1.Text=“aaa”),這樣一來,界面變化后,后端邏輯代碼也需要做相應(yīng)的變更。

在WPF中使用數(shù)據(jù)綁定機(jī)制,當(dāng)數(shù)據(jù)變化后,數(shù)據(jù)會通知界面變更的發(fā)生,而不需要通過訪問界面元素來修改值,這樣在后端邏輯代碼中也就不必操作或者很少操作界面的元素了。

使用MVVM,可以很好的配合WPF的數(shù)據(jù)綁定機(jī)制來實現(xiàn)UI與邏輯代碼的分離,MVVM中的View表示界面,負(fù)責(zé)頁面顯示,ViewModel負(fù)責(zé)邏輯處理,包括準(zhǔn)備綁定的數(shù)據(jù)和命令,ViewModel通過View的DataContext屬性綁定至View,Model為業(yè)務(wù)模型,供ViewModel使用。

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

推薦閱讀更多精彩內(nèi)容