本文主要介紹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);
-
}
-
}
-
}
例子--轉摘