wpf mvvm 实例
1.程序結(jié)構(gòu)如圖所示:
2.Model實(shí)現(xiàn)
在Model文件夾下新建業(yè)務(wù)類StudentModel,代碼如下:
public class StudentModel : INotifyPropertyChanged
{
private int studentId;
public int StudentId
{
get { return studentId; }
set
{
studentId = value;
NotifyPropertyChanged("StudentId");
}
}
private string studentName;
public string StudentName
{
get { return studentName; }
set { studentName = value; }
}
private int studentAge;
public int StudentAge
{
get { return studentAge; }
set { studentAge = value; }
}
private string studentEmail;
public string StudentEmail
{
get { return studentEmail; }
set { studentEmail = value; }
}
private string studentSex;
public string StudentSex
{
get { return studentSex; }
set { studentSex = value; }
}
public event PropertyChangedEventHandler PropertyChanged;
public void NotifyPropertyChanged(string propertyName)
{
if (propertyName != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}
}
StudentModel類實(shí)現(xiàn)了接口INotifyPropertyChanged。當(dāng)類實(shí)現(xiàn)該接口后,便可以執(zhí)行綁定的客戶端發(fā)出某一屬性值已改變的通知。
3ViewModel實(shí)現(xiàn)
代碼如下:
public class StudentViewModel
{
public DelegateCommand ShowCommand { get; set; }
public StudentModel Student { get; set; }
public StudentViewModel()
{
Student = new StudentModel();
ShowCommand = new DelegateCommand();
ShowCommand.ExecuteCommand = new Action<object>(ShowStudentData);
}
private void ShowStudentData(object obj)
{
Student.StudentId = 1;
Student.StudentName = "tiana";
Student.StudentAge = 20;
Student.StudentEmail = "456456646@qq.com";
Student.StudentSex = "男";
}
}
public class DelegateCommand : ICommand
{
public Action<object> ExecuteCommand = null;
public Func<object, bool> CanExecuteCommand = null;
public event EventHandler CanExecuteChanged;
public bool CanExecute(object parameter)
{
if (CanExecuteCommand != null)
{
return this.CanExecuteCommand(parameter);
}
else
{
return true;
}
}
public void Execute(object parameter)
{
if (this.ExecuteCommand != null)
{
this.ExecuteCommand(parameter);
}
}
public void RaiseCanExecuteChange()
{
if (CanExecuteChanged != null)
{
CanExecuteChanged(this, EventArgs.Empty);
}
}
}
代碼中,除了定義StudentViewModel類外,還定義了DelegateCommand類,該類實(shí)現(xiàn)了ICommand接口。
ICommand接口中的Execute()方法用于命令的執(zhí)行,CanExecute()方法用于只是當(dāng)前命令在目標(biāo)元素上是否可用,當(dāng)這種可用性發(fā)生改變時便會觸發(fā)接口中的CanExecuteChanged事件。
我們可以將實(shí)現(xiàn)了ICommand接口命令DelegateCommand賦值給Button(命令源)的Command屬性(只有實(shí)現(xiàn)了ICommandSource接口的元素才擁有該屬性),這樣Button便與命令進(jìn)行了綁定。
?
MainWindow界面的xaml代碼如下:
<Window x:Class="WPFMVVMExample.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:WPFMVVMExample"
mc:Ignorable="d"
Title="MainWindow" Height="350" Width="525">
<Grid>
<Label Content="學(xué)號" Height="28" HorizontalAlignment="Left" Margin="54,23,0,0" Name="labelStudentId" VerticalAlignment="Top"></Label>
<TextBox Text="{Binding Student.StudentId}" IsReadOnly="True" Height="23" HorizontalAlignment="Right" Margin="0,27,289,0" Name="textBoxStudentId" VerticalAlignment="Top" Width="120"></TextBox>
<Button Command="{Binding ShowCommand}" Content="顯示" Height="23" HorizontalAlignment="Left" Margin="345,27,0,0" Name="buttonShow" VerticalAlignment="Top" Width="75"></Button>
</Grid>
</Window>
MainWindow后端代碼如下:
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
this.DataContext = new StudentViewModel();
}
}
}
5運(yùn)行程序
運(yùn)行程序,點(diǎn)擊“”顯示“按鈕”,即將數(shù)據(jù)綁定至界面顯示
6.說明
wpf中使用mvvm可以降低UI顯示與后端邏輯代碼的耦合度,即便換界面時,只需要修改很少的邏輯代碼就可以實(shí)現(xiàn),甚至不用修改。
在wpf中使用數(shù)據(jù)綁定機(jī)制,當(dāng)數(shù)據(jù)變化后,數(shù)據(jù)會通知界面變更的發(fā)生,而不需要通過訪問界面元素來修改值,這樣后端邏輯代碼中也就不必操作或者很少操作界面的元素了。
使用MVVM,可以很好的配合WPF的數(shù)據(jù)綁定機(jī)制來實(shí)現(xiàn)ui與邏輯代碼的分離,mvvm中的view表示界面,負(fù)責(zé)頁面顯示,viewmodel負(fù)責(zé)邏輯處理,包括準(zhǔn)備綁定的數(shù)據(jù)和命令,viewmodel通過view的datacontext屬性綁定至view,model為業(yè)務(wù)模型,共viewmodel使用。
7.架構(gòu)
如果你讀到這里,你會發(fā)現(xiàn),如果你以后要使用這種方式,以上的代碼大多都是在重復(fù):實(shí)現(xiàn)INPC(INotifyPropertyChanged)或創(chuàng)建Command這其實(shí)就是大部分MVVM 的樣板,所以我們可以將實(shí)現(xiàn)INPC放到一個基類中,我們把它叫做“ObservableObject”。對于RelayCommand類,我們可以將其移動到我們的.net類庫中。這就是所有的mvvm框架開始所做的(如Prism,Caliburn)。
?
轉(zhuǎn)載于:https://www.cnblogs.com/v-haoz/p/9400033.html
總結(jié)
以上是生活随笔為你收集整理的wpf mvvm 实例的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: IDApython 命令
- 下一篇: 关于爬虫的日常复习(8)—— 实战:re