基于WPF系统框架设计(6)-整合MVVM框架(Prism)

摘要:
//合成wpf。codeplex。com/original模式(Winform)(1)现在让我们看看以前的设计视图:usingSystem。收藏。通用的使用System.Linq;使用System.Text;使用System.Windows.Controls;使用System.Windows.Media;
应用场景

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

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

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

原始的模式(Winform)

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

image

(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一致,两者相互依赖,分不开了,动其中一个都会有问题,使编译出错。

image

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

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

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

导入MVVM模式

步骤1:

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

解决方案目录如下图:

image

步骤2:

  1. MainWindowViewModel这个类继承NotificationObject。

image

注意下图显示: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="Imagesclose.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();
            }
        }
    }
}

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

image

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

image

这样,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)》仅用于学习参考。如对内容有疑问,请及时联系本站处理。

上篇Spring框架之jdbc源码完全解析Navicat 常用快捷键下篇

宿迁高防,2C2G15M,22元/月;香港BGP,2C5G5M,25元/月 雨云优惠码:MjYwNzM=

相关文章

Vue之日历控件vue-full-calendar的使用

(1).安装依赖 npm install vue-full-calendar  npm install moment 因为这是日历插件用到了时间工具类 === moment  (2).文件中导入依赖 在想要用此插件的文件中导入依赖 import { FullCalendar } from 'vue-full-calendar' import "f...

boost正则库匹配ASII编码的中文、全角字符示例

首先,boost正则库(regex)不支持形如 [0-9] 这样的表达式,貌似会崩溃。    现在查网上正则匹配中文的例子,都是讲用 \uFF00-\uFFFF ; 拜托,\u是unicode编码,能用于我常用的ansii编码(如GB18030,GBK,GB2312等等)吗?举例时也不说清楚。    再次查看正则语法,发现 \xnn 比较有用,\x匹配AS...

Linux设备驱动模型(sysfs)

<总线模型概述> 随着技术的发展,系统的拓扑结构也越来越复杂,对热插拔。跨平台移植性的要求越来越高,从Linux2.6内核开始提供全新的设备模型。将所有的驱动挂载到计算机的总线上(比如USB总线),当有设备连接到总线上的时候,总线能够感知到,这时系统就会把挂载到总线上是所有驱动和设备匹配。通过不同的识别方式,知道找到相应的驱动。   <...

Navicat Premuim远程连接oracle 提示 cannot load oci dll,193的解决方法

转载:http://blog.51cto.com/xiao987334176/1640991 内网有一台windows server 2012,安装了Navicat 11.1.8 连接oracle的时候,提示 cannot load oci dll,193:D:Program Files (x86)PremiumSoftNavicat Premium***...

WPF样式学习第一天

因为上班的公司要我用wpf写软件,刚毕业出来,学校也没教过wpf,只会winform,再加上wpf用winform的框架写法也能实现很多需求,但是就不能体现wpf的优点了,今天就先来学wpf的样式,因为对美的认识不足,所以排版不好,对代码有意见或建议的,希望多多提出,我是初学者。 今天就先弄个Button的简单样式吧 ButtonStyle是源名称。 P...

[转载]目前流行的缺陷管理工具

缺陷管理工具:1.  Bugzilla2.  Bugfree3.  TestDirector (Quality Center)4.  ClearQuest5.  JIRA6.  Mantis7.  Bugzero8. BugTracker9. URTracker10.KisTracker11.TestLink12、JTrac13、BugNet14、BugO...