日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > c/c++ >内容正文

c/c++

WinForm中的MVC模式--MVP模式

發布時間:2025/5/22 c/c++ 23 豆豆
生活随笔 收集整理的這篇文章主要介紹了 WinForm中的MVC模式--MVP模式 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

本文主要介紹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模式的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。