本文主要介绍MVC模式在WINFORM中的实现,其实砖家们都称它为MVP模式,小弟E文不太好,真的是记不住那个P怎么拼写的。。

MVC模式主要解决的问题就是将表示层和业务层进行分离,在以往做WINFORM项目的时候,通常都是将很多的逻辑代码直接写在了Form.cs代码的事件里,这样的话业务逻辑就和界面紧耦合在一起了,现在我们采用MVC来解耦。

首先建立Model:


  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.ComponentModel;
  6. namespace WindowsFormsApplication10
  7. {
  8. public class Person : INotifyPropertyChanged
  9. {
  10. private string _id;
  11. public string ID
  12. {
  13. get { return _id; }
  14. set { _id = value; OnPropertyChanged("ID"); }
  15. }
  16. private string _name;
  17. public string Name
  18. {
  19. get { return _name; }
  20. set { _name = value; OnPropertyChanged("Name"); }
  21. }
  22. #region INotifyPropertyChanged 成员
  23. public event PropertyChangedEventHandler PropertyChanged;
  24. protected void OnPropertyChanged(string PropertyName)
  25. {
  26. PropertyChangedEventHandler handler = PropertyChanged;
  27. if (handler != null)
  28. {
  29. handler(this, new PropertyChangedEventArgs(PropertyName));
  30. }
  31. }
  32. #endregion
  33. }
  34. }

为了能支持双向绑定数据,Model实现了INotifyPropertyChanged接口.

再看看Controllor的实现:


  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. namespace WindowsFormsApplication10
  6. {
  7. public class PersonControllor
  8. {
  9. public PersonForm View;
  10. public Person Model;
  11. public PersonControllor(PersonForm view)
  12. {
  13. //初始化了一个Model
  14. Model = new Person() { ID = "1", Name = "xiaojun" };
  15. //通过构造函数将View注入到Controllor中
  16. this.View = view;
  17. //建立起View 和Controllor的关联
  18. //这时候View中能使用它所对应的Controllor进行业务逻辑的操作,Model也能和VIEW UI控件进行双向绑定
  19. this.View.Controllor = this;
  20. }
  21. /// <summary>
  22. /// 执行一个业务逻辑
  23. /// </summary>
  24. public void UpdatePerson()
  25. {
  26. UpdateToDataBase(Model);
  27. }
  28. private void UpdateToDataBase(Person p)
  29. {
  30. //do some thing
  31. //执行将数据插入到数据库的操作
  32. System.Windows.Forms.MessageBox.Show("ID:" + p.ID + " Name:" + p.Name);
  33. }
  34. }
  35. }

然后是View的实现:


  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Windows.Forms;
  9. namespace WindowsFormsApplication10
  10. {
  11. public partial class PersonForm : Form
  12. {
  13. private PersonControllor _controllor;
  14. public PersonControllor Controllor
  15. {
  16. get { return _controllor; }
  17. set
  18. {
  19. this._controllor = value;
  20. //绑定一定只能写在给Controllor赋值以后而不能写在PersonForm的构造函数中(此时Controllor还未被实例化)
  21. //因为我们这里采用的是Controllor-First而不是View-First,不然Controllor.Model为null会异常
  22. //将View通过构造函数注入到Controllor中的属于Controllor-First,这时候Controllor先创建
  23. //将Controllor通过构造函数注入到View中的属于View-First,这时候View先创建
  24. this.textBox1.DataBindings.Add("Text", Controllor.Model, "ID");
  25. this.textBox2.DataBindings.Add("Text", Controllor.Model, "Name");
  26. }
  27. }
  28. public PersonForm()
  29. {
  30. InitializeComponent();
  31. }
  32. private void button1_Click(object sender, EventArgs e)
  33. {
  34. //改变VIEW的UI控件的值,Controllor的Model会跟着变
  35. this.textBox1.Text = "2";
  36. this.textBox2.Text = "jacky";
  37. Controllor.UpdatePerson();
  38. }
  39. private void button2_Click(object sender, EventArgs e)
  40. {
  41. //改变Controllor的Model的值,VIEW的UI控件的值会跟着变
  42. Controllor.Model.ID = "2";
  43. Controllor.Model.Name = "jacky";
  44. Controllor.UpdatePerson();
  45. }
  46. private void PersonForm_Load(object sender, EventArgs e)
  47. {
  48. }
  49. }
  50. }

最后是程序启动:


  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Windows.Forms;
  5. namespace WindowsFormsApplication10
  6. {
  7. static class Program
  8. {
  9. /// <summary>
  10. /// 应用程序的主入口点。
  11. /// </summary>
  12. [STAThread]
  13. static void Main()
  14. {
  15. Application.EnableVisualStyles();
  16. Application.SetCompatibleTextRenderingDefault(false);
  17. //Controllor-First模式,先创建Controllor(PersonControllor)再将View(PersonForm)注入到Controllor(PersonControllor)中
  18. PersonControllor controllor = new PersonControllor(new PersonForm());
  19. Application.Run(controllor.View);
  20. }
  21. }
  22. }

例子--转摘

WinForm中的MVC模式--MVP模式的更多相关文章

  1. 前端开发中的 MVC、MVP、MVVM 模式

    MVC,MVP和MVVM都是常见的软件架构设计模式(Architectural Pattern),它通过分离关注点来改进代码的组织方式.不同于设计模式(Design Pattern),只是为了解决一类 ...

  2. Android中的MVC,MVP和MVVM

    韩梦飞沙  韩亚飞  313134555@qq.com  yue31313  han_meng_fei_sha MVC,MVP,MVVM的区别 #MVC 软件可以分为三部分 视图(View):用户界面 ...

  3. MVP模式在Android项目中的使用

    以前在写项目的时候,没有过多考虑架构模式的问题,因为之前一直做J2EE开发,而J2EE都是采用MVC模式进行开发的,所以在搭建公司项目的时候,也是使用类似MVC的架构(严格来讲,之前的项目还算不上MV ...

  4. MVP模式在Android开发中的应用

    一.MVP介绍      随着UI创建技术的功能日益增强,UI层也履行着越来越多的职责.为了更好地细分视图(View)与模型(Model)的功能,让View专注于处理数据的可视化以及与用户的交互.同一 ...

  5. MVP模式在Android中的使用

    转载了一篇博客.博客来自:http://www.liuling123.com/2015/12/mvp-pattern-android.html 觉得博主写的非常好 曾经在写项目的时候.没有过多考虑架构 ...

  6. MVC、MVP、MVVM模式的概念与区别

    1. MVC框架 MVC全名是Model View Controller,是模型(model)-视图(view)-控制器(controller)的缩写,一种软件设计典范,用一种业务逻辑.数据.界面显示 ...

  7. .Net平台-MVP模式初探(一)

    为什么要写这篇文章 笔者当前正在负责研究所中一个项目,这个项目基于.NET平台,初步拟采用C/S部署体系,所以选择了Windows Forms作为其UI.经过几此迭代,我们发现了一个问题:虽然业务逻辑 ...

  8. MVP模式(Android)

    以前在写项目的时候,没有过多考虑架构模式的问题,因为之前一直做J2EE开发,而J2EE都是采用MVC模式进行开发的,所以在搭建公司项目的时候,也是使用类似MVC的架构(严格来讲,之前的项目还算不上MV ...

  9. 【Android - 框架】之MVP模式的使用

    提起MVP架构模式,大家可能首先想到的是它的"前辈"MVC模式.MVC由Model.View.Controller组成,请求从Controller进入后进行业务判断,然后交给Mod ...

  10. MVP模式入门案例

    随着UI创建技术的功能日益增强,UI层也履行着越来越多的职责.为了更好地细分视图(View)与模型(Model)的功能,让View专注于处理数据的可视化以及与用户的交互,同时让Model只关系数据的处 ...

随机推荐

  1. div css 页面中心弹窗窗口

    <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8&quo ...

  2. Perl报错you may need to install the Win32::Console module(转)

    ActivePerl-5.26.3.XXXX.msi安装后,命令行执行cpan,会出现如下提示而无法继续. Can't locate Win32/Console.pm in @INC (you may ...

  3. phaclon 初学者遇到的问题!

    1,框架安装  需要安装PHALCON扩展. 2,Nginx伪静态 配置 3,app.ini  常量配置等配置 4,主体目录结构 互相调用及 类的注册服务 依赖注入 自动加载项问题. 5,数据库相关操 ...

  4. Django设计模式(MVC/MVT)

    Django设计模式(MVC/MVT) 目的:了解什么是MVC,MVT 思考:为什么要用设计模式 分工.解耦,让不同的代码块之间降低耦合,增强代码的可扩展性和可移植性,实现向后兼容. MVC: M全拼 ...

  5. 第四章:基本Git概念(重点)

    本章通过讨论Git的基本架构组成和一些重要概念,来探讨Git的不同之处和原因. 一: 基本概念 1.版本库. 1]Git版本库只是一个简单的数据库,包含所有用来维护与管理项目的修订版本和历史记录. 2 ...

  6. NIO 缓冲区 ByteBuffer 之黏包和半包

    一.低效率方式 /** * 黏包.半包 */ private static void buffExample2() { /* 网络上传输多条数据给服务器,数据之间使用 \n 分隔. 但由于某种原因(多 ...

  7. PAT-basic-1026 程序运行时间 java

    一.题目 要获得一个 C 语言程序的运行时间,常用的方法是调用头文件 time.h,其中提供了 clock() 函数,可以捕捉从程序开始运行到 clock() 被调用时所耗费的时间.这个时间单位是 c ...

  8. spider_使用request库进行post传参

    # from urllib import parse,request# import jsonimport requests# 有道翻译urlurl = "http://fanyi.youd ...

  9. windows 和 linux 之间的文件传输操作

    参考:http://www.jb51.net/article/123378.htm 本文介绍,如何实现 windows 主机与 linux 虚拟机之间的文件传输. 一.获取虚拟机 IP 地址 要实现 ...

  10. 七牛云服务器debug

    400{ResponseInfo:com.qiniu.http.Response@16fd3806,status:400, reqId:uzcAADGFlzHUE-kW, xlog:-, xvia:, ...