MVC模式主要解决的问题就是将表示层和业务层进行分离,在以往做WINFORM项目的时候,通常都是将很多的逻辑代码直接写在了Form.cs代码的事件里,这样的话业务逻辑就和界面紧耦合在一起了,现在我们采用MVC来解耦。
首先建立Model:
- usingSystem;
-
usingSystem.Collections.Generic;
-
usingSystem.Linq;
-
usingSystem.Text;
-
usingSystem.ComponentModel;
-
namespaceWindowsFormsApplication10
- {
-
publicclassPerson:INotifyPropertyChanged
- {
-
privatestring_id;
-
publicstringID
- {
-
get{return_id;}
-
set{_id=value;OnPropertyChanged("ID");}
- }
-
privatestring_name;
-
publicstringName
- {
-
get{return_name;}
-
set{_name=value;OnPropertyChanged("Name");}
-
}
- #regionINotifyPropertyChanged成员
-
publiceventPropertyChangedEventHandlerPropertyChanged;
-
protectedvoidOnPropertyChanged(stringPropertyName)
- {
- PropertyChangedEventHandlerhandler=PropertyChanged;
-
if(handler!=null)
- {
-
handler(this,newPropertyChangedEventArgs(PropertyName));
- }
-
}
- #endregion
- }
- }
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的实现:
- usingSystem;
-
usingSystem.Collections.Generic;
-
usingSystem.Linq;
-
usingSystem.Text;
-
namespaceWindowsFormsApplication10
- {
-
publicclassPersonControllor
- {
-
publicPersonFormView;
-
publicPersonModel;
-
publicPersonControllor(PersonFormview)
- {
-
-
Model=newPerson(){ID="1",Name="xiaojun"};
-
-
this.View=view;
-
-
-
this.View.Controllor=this;
- }
-
-
-
-
publicvoidUpdatePerson()
- {
- UpdateToDataBase(Model);
- }
-
privatevoidUpdateToDataBase(Personp)
- {
-
-
-
System.Windows.Forms.MessageBox.Show("ID:"+p.ID+"Name:"+p.Name);
- }
- }
- }
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的实现:
- usingSystem;
-
usingSystem.Collections.Generic;
-
usingSystem.ComponentModel;
-
usingSystem.Data;
-
usingSystem.Drawing;
-
usingSystem.Linq;
-
usingSystem.Text;
-
usingSystem.Windows.Forms;
-
namespaceWindowsFormsApplication10
- {
-
publicpartialclassPersonForm:Form
- {
-
privatePersonControllor_controllor;
-
publicPersonControllorControllor
- {
-
get{return_controllor;}
-
set
- {
-
this._controllor=value;
-
-
-
-
-
this.textBox1.DataBindings.Add("Text",Controllor.Model,"ID");
-
this.textBox2.DataBindings.Add("Text",Controllor.Model,"Name");
- }
- }
-
publicPersonForm()
- {
- InitializeComponent();
- }
-
privatevoidbutton1_Click(objectsender,EventArgse)
- {
-
-
this.textBox1.Text="2";
-
this.textBox2.Text="jacky";
- Controllor.UpdatePerson();
- }
-
privatevoidbutton2_Click(objectsender,EventArgse)
- {
-
-
Controllor.Model.ID="2";
-
Controllor.Model.Name="jacky";
- Controllor.UpdatePerson();
- }
-
privatevoidPersonForm_Load(objectsender,EventArgse)
- {
- }
- }
- }
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)
{
}
}
}
最后是程序启动:
- usingSystem;
-
usingSystem.Collections.Generic;
-
usingSystem.Linq;
-
usingSystem.Windows.Forms;
-
namespaceWindowsFormsApplication10
- {
-
staticclassProgram
- {
-
-
-
- [STAThread]
-
staticvoidMain()
- {
- Application.EnableVisualStyles();
-
Application.SetCompatibleTextRenderingDefault(false);
-
-
PersonControllorcontrollor=newPersonControllor(newPersonForm());
- Application.Run(controllor.View);
- }
- }
- }
分享到:
相关推荐
Retrofit+MVP模式 最最简单的案例 若有错误请指出
很简单的一款mvp模式,简单易懂,适合初学者所学
MVP模式计算器事例,网络上找到的资源,MVP模式开发,web,winform切换非常容易。
一个MVP的Demo,展示了在iOS平台上如何实现MVP模式 希望通过这个项目能让大家了解如何在iOS平台上实现MVP模式
源代码解析MVP模式 很好的学习资料,推荐!
android mvp设计模式demo。官方源码实现mvp模式,结构清晰,一目了然。
MVP模式安卓开发案例
该项目快捷简洁的实现了Android mvp模式,非常明了易懂
android MVP模式例子源码,封装了一个对数据库操作的类,实现了对数据的进行数据库的存取,具体MVP模式教程参考博客http://blog.csdn.net/u012925323/article/details/50409422,希望对大家有帮助。
android MVP模式的简单练习,可以由此比较好的理解这个模式。
一个简单的android mvp模式,帮助新手理解mvp模式。简单的登录。
使用mvp模式实现的用户登录和注册功能
Android MVP模式实现登录操作.zip,太多无法一一验证是否可用,程序如果跑不起来需要自调,部分代码功能进行参考学习。
ASP.NET 的MVP模式简单实现,采用被动视图,Model层依赖注入实现,本来想用linq呢,但是方便移植起见,用了xml,别忘了自己修改配置文件的数据库地址
标准的MVP模式的使用,请求天气数据的Demo
基于MVP模式的医疗科普App设计与实现.pdf
MVP模式,MVP所对应的意义:M-Model-模型、V-View-视图、P-Presenter-表示器。 博客地址:http://blog.csdn.net/dickyqie/article/details/54894396
MVP模式
在安卓上运用的MVP模式开发框架。A framework for MVP pattern in android. 与传统MVP开发模式相反,EasyMVP是将activity,fragment, adapter作为presenter的mvp开发框架。 了解更多 可以看看这篇文章对这种模式的...