应用场景

我们基础的框架已经搭建起来了,现在整合MVVM框架Prism,在ViewModel做一些逻辑处理,真正把界面设计分离出来。

这样方便我们系统开发分工合作,同时提高系统可维护性和灵活性。

具体的Prism安装和Microsoft.Practices.Prism.dll获取,在这个网址:http://compositewpf.codeplex.com/

原始的模式(Winform)

(1)现在看一下之前的设计的View: MainWindow.XAML源码:

(2)MainWindow.xaml.cs源码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using Fluent;
using Xceed.Wpf.AvalonDock.Layout; namespace TLAgent.SecurityManager.WPF
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : RibbonWindow
{
public MainWindow()
{
InitializeComponent();
}
private void OnExitSystem(object sender, RoutedEventArgs e)
{
MessageBoxResult result = MessageBox.Show("确定要退出系统吗?", "确认消息", MessageBoxButton.OKCancel, MessageBoxImage.Question);
if (result == MessageBoxResult.OK)
{
Application.Current.Shutdown();
}
}
}
}

传统模式问题在哪里?

现在如果我们变一下View的Click事件名称OnExitSystem”,就直接报错了,因为这个事件名称必须跟View后台代码中的OnExitSystem一致,两者相互依赖,分不开了,动其中一个都会有问题,使编译出错。

同样,如果我们把这个View删除,那后台代码的逻辑也一样被删除了,以前编写的逻辑都没有了。

我们如何能够达到两者分离,使逻辑部分功能能够很好地复用?

这就是我们导入MVVM要解决的问题。

导入MVVM模式

步骤1:

  1. 在项目中引入Microsoft.Practices.Prism.dll.
  2. 新建几个目录:Models,ViewModels
  3. 在ViewModels下新建一个类跟MainWindow.xaml对应的ViewModel:MainWindowViewModel.cs

解决方案目录如下图:

步骤2:

  1. 让MainWindowViewModel这个类继承NotificationObject。

注意下图显示:NotificationObject是来自Microsoft.Practices.Prism.ViewModel。

定义一个委托命令:DelegateCommand命名为ExitSystemCommand,并把它设为可读写。

在构造函数中实例化这个对象,并给它绑定一个方法OnExit,源码如下:

using Microsoft.Practices.Prism.ViewModel;

namespace TLAgent.SecurityManager.WPF.ViewModels
{
public class MainWindowViewModel : NotificationObject
{
public DelegateCommand ExitSystemCommand { get; set; } public MainWindowViewModel()
{
ExitSystemCommand = new DelegateCommand(this.OnExit);
} private void OnExit()
{
MessageBoxResult result = MessageBox.Show("确定要退出系统吗?", "确认消息", MessageBoxButton.OKCancel, MessageBoxImage.Question);
if (result == MessageBoxResult.OK)
{
Application.Current.Shutdown();
}
}
}
}

前台View用Command绑定这个委托命令ExitSystemCommand :

<!--Backstage Items-->
<Fluent:Ribbon.Menu>
<Fluent:Backstage Background="RoyalBlue">
<Fluent:BackstageTabControl Background="RoyalBlue">
<Fluent:Button Header="退出系统" Command="{Binding ExitSystemCommand}" Icon="Images\close.png"/>
</Fluent:BackstageTabControl>
</Fluent:Backstage>
</Fluent:Ribbon.Menu>

可是把程序运行点击还是没有效果,还有关键的一步,加如下一行代码:

using System.Windows;
using Fluent;
using TLAgent.SecurityManager.WPF.ViewModels; namespace TLAgent.SecurityManager.WPF
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : RibbonWindow
{
public MainWindow()
{
InitializeComponent();
this.DataContext = new MainWindowViewModel();
} private void OnExitSystem(object sender, RoutedEventArgs e)
{
MessageBoxResult result = MessageBox.Show("确定要退出系统吗?", "确认消息", MessageBoxButton.OKCancel, MessageBoxImage.Question);
if (result == MessageBoxResult.OK)
{
Application.Current.Shutdown();
}
}
}
}

运行一下程序,点击“退出系统”按钮,效果出来了~~~

另外:this.DataContext = new MainWindowViewModel(); 也可以在View层的XAML里面写,参考如下:

这样,MainWindow 里的其他源码就可以清掉了,反原初始的默认状态。

using System.Windows;
using Fluent;
using TLAgent.SecurityManager.WPF.ViewModels; namespace TLAgent.SecurityManager.WPF
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : RibbonWindow
{
public MainWindow()
{
InitializeComponent();
}
}
}

这一阶段只整合一个简单的示例,后续会添加更复杂ViewModel层的对View层控件的操作等功能。

示例源码

基于WPF系统框架设计(6)-整合MVVM框架(Prism)的更多相关文章

  1. 基于WPF系统框架设计(5)-Ribbon整合Avalondock 2.0实现多文档界面设计(二)

    AvalonDock 是一个.NET库,用于在停靠模式布局(docking)中排列一系列WPF/WinForm控件.最新发布的版本原生支持MVVM框架.Aero Snap特效并具有更好的性能. Ava ...

  2. 整合MVVM框架(Prism)

    整合MVVM框架(Prism) 我们基础的框架已经搭建起来了,现在整合MVVM框架Prism,在ViewModel做一些逻辑处理,真正把界面设计分离出来. 这样方便我们系统开发分工合作,同时提高系统可 ...

  3. 基于WPF系统框架设计(4)-Ribbon整合Avalondock 2.0实现多文档界面设计(一)

    前些时间研究了WPF的一些框架,感觉基于Prism框架的MVVM模式对系统的UI与逻辑分离很好,所以就按照之前Winform的框架设计,用WPF做了一套,感觉比Winform要强很多. MVVM模式和 ...

  4. 基于WPF系统框架设计(3)-Fluent Ribbon界面布局

    一个系统框架除了功能菜单导航,有系统内容显示区域,系统状态栏. Silver: Blue: Black: 系统界面设计,就不进行技术细节介绍了,主题以框架设计为主,Xaml源码参考: <Flue ...

  5. 基于WPF系统框架设计(1)-为什么要仿Office2010 Ribbon?

    为什么系统框架设计使用Ribbon导航模式? 这得从Office软件的演变说起.微软为什么最后选择使用Ribbon,也许就是很多系统设计要使用Ribbon做功能导航的原因. 你是否还记得曾经使用过的M ...

  6. 基于WPF系统框架设计(2)-Fluent Ribbon之HelloWorld

    Fluent/Ribbon是微软在其最新桌面操作系统Windows 7中使用的图形用户界面. Windows平台的进化,伴随着系统图形界面的重新设计.从Windows XP到Windows Vista ...

  7. 基于WPF系统框架设计(10)-分页控件设计

    背景 最近要求项目组成员开发一个通用的分页组件,要求是这个组件简单易用,通用性,兼容现有框架MVVM模式,可是最后给我提交的成果勉强能够用,却欠少灵活性和框架兼容性. 设计的基本思想 传入数据源,总页 ...

  8. 基于WPF系统框架设计(8)-PasswordBox传值到ViewMode

    应用场景 我要做一个系统登录功能,需要传用户名和密码到ViewModel中,可是PasswordBox传值到ViewModel中好像跟TextBox等控件不一样.这里需要用到附加属性. 附加属性:一个 ...

  9. 基于WPF系统框架设计(7)-TextBox/PasswordBox在ViewModel中支持回车命令

    应用场景 我现在做一个系统登录功能,要求在PasswordBox上输完密码后回车,能够响应Enter事件,并执行ViewModel中对应的方法.如果登录成功则隐藏当前窗口显示主窗体,登录失败则焦点返回 ...

随机推荐

  1. angular用$sce服务来过滤HTML标签

    angular js的强大之处之一就是他的数据双向绑定这一牛B功能,我们会常常用到的两个东西就是ng-bind和针对form的ng-model.但在我们的项目当中会遇到这样的情况,后台返回的数据中带有 ...

  2. gulp基本入门

    gulp用于自动化和提高工作流,类似于grunt.gulp适用于nodejs平台.   gulp基础: gulp两个主要的功能是读取想要处理的文件,把处理好的文件放到指定的地方gulp.src()找出 ...

  3. Python框架之Django学习笔记(二)

    安装Django 我是在windows下安装的python以及django,下面的链接可以下载Django: http://www.djangoproject.com/download/ 1.下载 D ...

  4. 9、JS对象 知识总结

    1.对象 <!DOCTYPE html> <html> <body> <script> <!-- 新建对象 --> person=new O ...

  5. 【Reverse Integer】cpp

    题目: Reverse digits of an integer. Example1: x = 123, return 321Example2: x = -123, return -321 click ...

  6. jmeter将上一个请求的结果作为下一个请求的参数——使用正则提取器

    转自:http://www.cnblogs.com/0201zcr/p/5089620.html 在压力测试的时候,经常要将几个流程串联起来才能将程序测试通过.如:我现在用户首先要登录,获得我登录的凭 ...

  7. CSU-2019 Fleecing the Raffle

    CSU-2019 Fleecing the Raffle Description A tremendously exciting raffle is being held, with some tre ...

  8. java中的读/写锁

    读写锁接口:ReadWriteLock,它的具体实现类为:ReentrantReadWriteLock 使用场景: 对于一个资源,读读能共存,读写不能共存,写写不能共存. 锁降级:从写锁变成读锁: 锁 ...

  9. iOS动画-扩散波纹效果

    最终效果 实现思路 动画的表现形式是颜色以及大小的变化,整体效果可以看做多个单独的波纹效果的叠加.因此我们可以创建多个CALayer,分别赋予CABasicAnimation动画,组成最终的动画效果. ...

  10. [NOI2012][bzoj2879] 美食节 [费用流+动态加边]

    题面 传送门 思路 先看看这道题 修车 仔细理解一下,这两道题是不是一样的? 这道题的不同之处 但是有一个区别:本题中每一种车有多个需求,但是这个好办,连边的时候容量涨成$p\lbrack i\rbr ...