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

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

首先建立Model:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel;
namespace WindowsFormsApplication10
{
public class Person : INotifyPropertyChanged
{
private string _id;
public string ID
{
get { return _id; }
set { _id = value; OnPropertyChanged("ID"); }
}
private string _name;
public string Name
{
get { return _name; }
set { _name = value; OnPropertyChanged("Name"); }
}
#region INotifyPropertyChanged 成员
public event PropertyChangedEventHandler PropertyChanged;
protected void OnPropertyChanged(string PropertyName)
{
PropertyChangedEventHandler handler = PropertyChanged;
if (handler != null)
{
handler(this, new PropertyChangedEventArgs(PropertyName));
}
}
#endregion
}
}

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

再看看Controllor的实现:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace WindowsFormsApplication10
{
public class PersonControllor
{
public PersonForm View;
public Person Model;
public PersonControllor(PersonForm view)
{
//初始化了一个Model
Model = new Person() { ID = "1", Name = "xiaojun" };
//通过构造函数将View注入到Controllor中
this.View = view;
//建立起View 和Controllor的关联
//这时候View中能使用它所对应的Controllor进行业务逻辑的操作,Model也能和VIEW UI控件进行双向绑定
this.View.Controllor = this;
}
/// <summary>
/// 执行一个业务逻辑
/// </summary>
public void UpdatePerson()
{
UpdateToDataBase(Model);
}
private void UpdateToDataBase(Person p)
{
//do some thing
//执行将数据插入到数据库的操作
System.Windows.Forms.MessageBox.Show("ID:" + p.ID + " Name:" + p.Name);
}
}
}

然后是View的实现:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace WindowsFormsApplication10
{
public partial class PersonForm : Form
{
private PersonControllor _controllor;
public PersonControllor Controllor
{
get { return _controllor; }
set
{
this._controllor = value;
//绑定一定只能写在给Controllor赋值以后而不能写在PersonForm的构造函数中(此时Controllor还未被实例化)
//因为我们这里采用的是Controllor-First而不是View-First,不然Controllor.Model为null会异常
//将View通过构造函数注入到Controllor中的属于Controllor-First,这时候Controllor先创建
//将Controllor通过构造函数注入到View中的属于View-First,这时候View先创建
this.textBox1.DataBindings.Add("Text", Controllor.Model, "ID");
this.textBox2.DataBindings.Add("Text", Controllor.Model, "Name");
}
}
public PersonForm()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
//改变VIEW的UI控件的值,Controllor的Model会跟着变
this.textBox1.Text = "2";
this.textBox2.Text = "jacky";
Controllor.UpdatePerson();
}
private void button2_Click(object sender, EventArgs e)
{
//改变Controllor的Model的值,VIEW的UI控件的值会跟着变
Controllor.Model.ID = "2";
Controllor.Model.Name = "jacky";
Controllor.UpdatePerson();
}
private void PersonForm_Load(object sender, EventArgs e)
{
}
}
}

最后是程序启动:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;
namespace WindowsFormsApplication10
{
static class Program
{
/// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
//Controllor-First模式,先创建Controllor(PersonControllor)再将View(PersonForm)注入到Controllor(PersonControllor)中
PersonControllor controllor = new PersonControllor(new PersonForm());
Application.Run(controllor.View);
}
}
}

例子--转摘

转载于:https://www.cnblogs.com/kingkie/p/9456075.html

WinForm中的MVC模式--MVP模式相关推荐

  1. 简析Android中的MVC、MVP架构

    MVC MVC是指Modle,View和Controller,将界面,业务逻辑和控制器分开,是一种低耦合的设计方式,适用于简单应用开发.举个简单的例子.android中的各种控件,即为View.例如, ...

  2. Android中RxJava+Retrofit2.0+MVP模式的整合

    版权声明:本文为博主原创文章,未经博主允许不得转载. https://blog.csdn.net/u010046908/article/details/50781360 转载请标明出处:http:// ...

  3. android 适合mvp模式,Android中的MVP:如何使Presenter层系统化?

    MVP(Model View Presenter)模式是著名的 MVC(Model View Controller)的衍生物,并且是 Android 应用程序中管理表示层的***的模式之一. 这篇文章 ...

  4. android中的mvp模式怎么定义,详解MVP模式在Android开发中的应用

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

  5. 浅谈 MVC、MVP 和 MVVM 架构模式

    2019独角兽企业重金招聘Python工程师标准>>> 谈谈 MVX 中的 Model 谈谈 MVX 中的 View 谈谈 MVX 中的 Controller 浅谈 MVC.MVP ...

  6. 一文带你全面了解MVC、MVP、MVVM模式(实例讲解)

    前言 在Android开发中,当你梳理完需求后,你要做的并不是马上写下你的第一行代码,而是需先设计好整个项目的技术框架 今天,我将全面介绍Android开发中主流的技术框架MVC.MVP 与 MVVM ...

  7. android中的mvp模式怎么定义,在android中使用MVP模式

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

  8. 这是一份全面 清晰的架构设计指南:MVC、MVP MVVM模式(含实例讲解)

    前言 在Android开发中,当你梳理完需求后,你要做的并不是马上写下你的第一行代码,而是需先设计好整个项目的技术框架 今天,我将全面介绍Android开发中主流的技术框架MVC.MVP 与 MVVM ...

  9. MVP模式的相关知识

    MVP 是从经典的模式MVC演变而来,它们的基本思想有相通的地方:Controller/Presenter负责逻辑的处理,Model提供数据,View负责显示.作为一种新的模式,MVP与MVC有着一个 ...

最新文章

  1. STM32系统滴答定时器(systick)应用
  2. 提交本地项目到github
  3. java线程间ThreadLocal的传递
  4. 简单的LRU Cache设计与实现
  5. [渝粤教育] 西南科技大学 现代制造系统 在线考试复习资料2021版
  6. MonoRail学习笔记十七:TransformFilter的使用
  7. Atitit 人脸识别 眼睛形态 attilax总结 可以按照大小来分类。。或者按照形态来分类 眼睛的类型、分类。包括杏眼,狐狸眼,铜铃眼,龙眼,丹凤眼和小鹿眼等等。 月牙眼 笑起来。。吊梢
  8. 基于MATLAB实现四阶龙格库塔法求解一、二阶微分方程实例
  9. 风暴数码论坛教程--厨房的搭建和简介
  10. 伦敦大学计算机研究生雅思要求,【留学科普】伦敦TOP10大学雅思要求来了!你够得上吗?...
  11. SAP培训学校的选择
  12. ffmpeg批量转换m4a为mp3
  13. Service Mesh介绍
  14. 对图片中的表格进行识别,并转换成excel文件(python、小软件)(批量)
  15. kubernetes continually evict pod when node's inode exhausted
  16. 如何强制卸载软件,强制卸载的工具。
  17. 商品详情页系统架构-笔记4-商品详情页多级缓存
  18. TMS320LF2407数字采样
  19. 基于java的校园新闻_基于jsp的校园新闻-JavaEE实现校园新闻 - java项目源码
  20. 【毕业季·进击的技术er】忆毕业一年有感

热门文章

  1. ROS系统 创建工作空间与功能包
  2. linux mariadb
  3. C利用time函数实现简单的定时器
  4. 项目第二周冲刺第六天
  5. webconfig中配置各种数据库的连接字符串(转)
  6. ECharts学习总结(五):echarts的Option概览
  7. Android图片360全景旋转
  8. POJ 1661 Help Jimmy(递推DP)
  9. 141 springmvc中 转发 与 重定向
  10. 两个简单的动态规划问题,0-1背包和最大不相邻数累加和,附递归c代码