WinForm中的MVC模式--MVP模式


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

 例子--轉摘


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM