WinForm中的MVC模式--MVP模式
生活随笔
收集整理的這篇文章主要介紹了
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的實現:
最后是程序啟動:
?例子--轉摘
轉載于:https://www.cnblogs.com/kingkie/p/9456075.html
總結
以上是生活随笔為你收集整理的WinForm中的MVC模式--MVP模式的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: hp笔记本u盘启动键怎么领导 hp笔记本
- 下一篇: Vue 使用 prerender-spa