本文主要介绍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. }

例子--转摘

最新文章

  1. js学习篇1--数组
  2. 【MVC】bootstrap-paginator 分页插件笔记
  3. 关于为busybox设置setuid
  4. mfc 在VC的两个对话框类中传递参数的三种方法
  5. Linux环境中DISPLAY环境变量的解释及设置
  6. 如何用火车头采集当前页面url网址
  7. 第六篇 Integration Services:初级工作流管理
  8. BZOJ1584 [Usaco2009 Mar]Cleaning Up 打扫卫生
  9. [JWFD开源工作流]JWFD开源工作流官方下载内容更新
  10. memcached全面剖析–3. memcached的删除机制和发展方向
  11. Java汉字排序(3)按笔划排序
  12. IIS 之 HTTP错误信息提示
  13. 展开/收缩 ul
  14. jsp中文乱码终极解决方法(转)
  15. javaTemplates-学习笔记四
  16. 如何学习Javascript ?
  17. 将程序添加到右键菜单和图标(以记事本、UltraEdit为例)
  18. 201521123076 《JAVA程序设计》第5周学习总结
  19. jquery实现链接的title快速出现
  20. Node.js Path 模块

热门文章

  1. css 动画基础配置说明
  2. Java 18-方法 认识方法与方法定义
  3. Activiti5.22.0扩展支持达梦数据库
  4. GIT 上传文件出错:fatal: Could not read from remote repository. 解决方案
  5. crontab执行不生效
  6. pgsql给表重命名
  7. 【mysql练习】转置,总计,分组
  8. Jquery_002
  9. 【相关杂项】stdio.h中的sprintf函数/union的作用
  10. 用python遍历一个图片文件夹,并输出所有路径到一个 txt 文件