您的位置:首页 > 其它

Prism MVVM实例

2014-03-09 17:26 113 查看
在《一个简单的WPF MVVM实例》一文中,我们手动实现了一个WPF MVVM实例程序,其实我们可以直接使用微软的Prism 来实现MVVM。

 

1 新建WPF 应用程序PrismMvvmExample

程序结构如下图所示。


 

 

2 添加Microsoft.Practices.Prism引用

下载Prism4(官方下载地址:http://compositewpf.codeplex.com/releases/view/55576

解压下载后的压缩包。

解压后,运行目录下的RegisterPrismBinaries.bat文件,这样就可以直接在VS中添加对Prism的引用。

具体方法如下图所示。

 


 


 

3 Model实现

在Model文件夹下新建业务类StudentModel(类文件StudentModel.cs),类的详细代码如下所示。

using Microsoft.Practices.Prism.ViewModel;

namespace PrismMvvmExample.Model
{
public class StudentModel : NotificationObject
{
/// <summary>
/// 学号
/// </summary>
private int studentId;
public int StudentId
{
get
{
return studentId;
}
set
{
studentId = value;
RaisePropertyChanged("StudentId");
}
}

/// <summary>
/// 姓名
/// </summary>
private string studentName;
public string StudentName
{
get
{
return studentName;
}
set
{
studentName = value;
RaisePropertyChanged("StudentName");
}
}

/// <summary>
/// 年龄
/// </summary>
private int studentAge;
public int StudentAge
{
get
{
return studentAge;
}
set
{
studentAge = value;
RaisePropertyChanged("StudentAge");
}
}

/// <summary>
/// Email
/// </summary>
private string studentEmail;
public string StudentEmail
{
get
{
return studentEmail;
}
set
{
studentEmail = value;
RaisePropertyChanged("StudentEmail");
}
}

/// <summary>
/// 性别
/// </summary>
private string studentSex;
public string StudentSex
{
get
{
return studentSex;
}
set
{
studentSex = value;
RaisePropertyChanged("StudentSex");
}
}
}
}
一个简单的WPF MVVM实例》一文中的StudentModel类实现了接口INotifyPropertyChanged,而本文中的StudentModel类继承的是NotificationObject类,该类是Microsoft.Practices.Prism程序集中定义的一个抽象类(命名空间为Microsoft.Practices.Prism.ViewModel),该类实现了INotifyPropertyChanged接口,所以StudentModel类本质上也是通过实现INotifyPropertyChanged接口来达到向执行绑定的客户端发出某一属性值已更改的通知。

详细代码可以使用Reflector来查看,这里就不给出详细代码。

 

3 ViewModel实现

在ViewModel文件夹下新建类文件StudentViewModel.cs,类文件的详细代码如下所示。

using System;
using Microsoft.Practices.Prism.Commands;
using Microsoft.Practices.Prism.ViewModel;
using PrismMvvmExample.Model;

namespace PrismMvvmExample.ViewModel
{
class StudentViewModel : NotificationObject
{
public DelegateCommand ShowCommand{get;set;}

public StudentViewModel()
{
ShowCommand = new DelegateCommand(new Action(ShowStudentInfo));
Student = new StudentModel();
}

private StudentModel student;
public StudentModel Student
{
get { return student; }
set
{
student = value;
}
}

private void ShowStudentInfo()
{
Student.StudentId = 1;
Student.StudentName = "tiana";
Student.StudentAge = 20;
Student.StudentEmail = "8644003248@qq.com";
Student.StudentSex = "大帅哥";
}

}
}
一个简单的WPF MVVM实例》一文中定义了一个实现ICommand接口的DelegateCommand类,而本文中直接使用了 DelegateCommand类而无需定义,那是因为 Microsoft.Practices.Prism程序集中定义了该类(命名空间为Microsoft.Practices.Prism.Commands),具体代码与《一个简单的WPF MVVM实例》一文中的定义类似。

 

4 MainWindow.xaml实现

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



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

<Window x:Class="PrismMvvmExample.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525">
<Grid>
<Label Content="学号" Height="28" HorizontalAlignment="Left" Margin="54,23,0,0" Name="labelStudentId" VerticalAlignment="Top" />
<TextBox Text="{Binding Student.StudentId}" IsReadOnly="True" Height="23" HorizontalAlignment="Right" Margin="0,27,289,0" Name="textBoxStudentId" VerticalAlignment="Top" Width="120" />
<Label Content="姓名" Height="28" HorizontalAlignment="Left" Margin="54,61,0,0" Name="labelStudentName" VerticalAlignment="Top" />
<TextBox Text="{Binding Student.StudentName}" IsReadOnly="True" Height="23" HorizontalAlignment="Left" Margin="94,65,0,0" Name="textBoxStudentName" VerticalAlignment="Top" Width="120" />
<Label Content="年龄" Height="28" HorizontalAlignment="Left" Margin="54,94,0,0" Name="labelStudentAge" VerticalAlignment="Top" />
<TextBox Text="{Binding Student.StudentAge}" IsReadOnly="True" Height="23" HorizontalAlignment="Left" Margin="94,99,0,0" Name="textBoxStudentAge" VerticalAlignment="Top" Width="120" />
<Label Content="Email" Height="28" HorizontalAlignment="Left" Margin="50,138,0,0" Name="labelStudentEmail" VerticalAlignment="Top" />
<TextBox Text="{Binding Student.StudentEmail}" IsReadOnly="True" Height="23" HorizontalAlignment="Left" Margin="94,141,0,0" Name="textBoxStudentEmail" VerticalAlignment="Top" Width="120" />
<Label Content="性别" Height="28" HorizontalAlignment="Left" Margin="57,176,0,0" Name="labelStudentSex" VerticalAlignment="Top" />
<TextBox Text="{Binding Student.StudentSex}" IsReadOnly="True" Height="23" HorizontalAlignment="Left" Margin="94,180,0,0" Name="textBoxStudentSex" VerticalAlignment="Top" Width="120" />
<Button Command="{Binding ShowCommand}" Content="显示" Height="23" HorizontalAlignment="Left" Margin="345,27,0,0" Name="buttonShow" VerticalAlignment="Top" Width="75" />
</Grid>
</Window>

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

using System.Windows;
using PrismMvvmExample.ViewModel;

namespace PrismMvvmExample
{
/// <summary>
/// MainWindow.xaml 的交互逻辑
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
this.DataContext = new StudentViewModel();
}
}
}

5 运行程序

运行程序,点击“显示”按钮,即将数据绑定至界面显示。

 


 

6 说明

WPF中使用MVVM可以降低UI显示与后端逻辑代码的耦合度,即更换界面时,只需要修改很少的逻辑代码就可以实现,甚至不用修改。

在WinForm开发中,我们一般会直接操作界面的元素(如:TextBox1.Text=“aaa”),这样一来,界面变化后,后端逻辑代码也需要做相应的变更。

在WPF中使用数据绑定机制,当数据变化后,数据会通知界面变更的发生,而不需要通过访问界面元素来修改值,这样在后端逻辑代码中也就不必操作或者很少操作界面的元素了。

使用MVVM,可以很好的配合WPF的数据绑定机制来实现UI与逻辑代码的分离,MVVM中的View表示界面,负责页面显示,ViewModel负责逻辑处理,包括准备绑定的数据和命令,ViewModel通过View的DataContext属性绑定至View,Model为业务模型,供ViewModel使用。

 

 

 

 
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息