当前位置:首页 > C#编程 > C#/.net框架 > 正文内容

C# winform的MVC设计模式

Jorge2年前 (2022-05-09)C#/.net框架1360

 

MVC模式主要解决的问题就是将表示层和业务层进行分离,在以往做WINFORM项目的时候,通常都是将很多的逻辑代码直接写在了Form.cs代码的事件里,这样的话业务逻辑就和界面紧耦合在一起了,现在我们采用MVC来解耦。这里写图片描述
这里写图片描述
首先建立Model:

C#
[csharp] view plain copyusing 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的实现:

C#
[csharp] view plain copyusing 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);  
        }  
  //这边的业务逻辑应该写在model里比较合适

    }  }

然后是View的实现:

C#
[csharp] view plain copyusing 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)  
        {  

        }  
    }  }

最后是程序启动:

C#
[csharp] view plain copyusing 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);  
        }  
    }  }


#转载请注明出处 www.skcircle.com 《少有人走的路》勇哥的工业自动化技术网站。

扫描二维码推送至手机访问。

版权声明:本文由7点博客发布,如需转载请注明出处。

本文链接:http://6dot.cn/?id=69

标签: .NET.NET框架
分享给朋友:

“C# winform的MVC设计模式” 的相关文章

索引器(C# 编程指南)

索引器(C# 编程指南)

 索引器允许类或结构的实例就像数组一样进行索引。 无需显式指定类型或实例成员,即可设置或检索索引值。 索引器类似于属性,不同之处在于它们的访问器需要使用参数。以下示例定义了一个泛型类,其中包含用于赋值和检索值的简单 get 和 set 访问器方法。&...

C# 当前不会命中断点 还没有为该文档加载任何符号

C# 当前不会命中断点 还没有为该文档加载任何符号

这个问题网上的经验大概如下:1。 清空方案,重新编译2。 删除项目bin目录下的东西,重新编译3。 解决相互引用的问题。4。 确保不是run的release5。把项目编译改为x866。 好像没发现其它的了。。。这些解决不了我们手上的项目的问题。我们的工程有几十个项目。我长话短说,解决方法是:引导项目...

关于C#项目引用的一点经验

关于C#项目引用的一点经验

关于项目引用,有几种:(一)这种是引用系统的程序集(二)下面这种是引用你自己的项目“解决方案”(三)最后一种是浏览本机上的项目的dll。对于工程中有几十个项目的软件来说,虽然使用(二)是很方便。但是会编译速度奇慢,而且随着项目越多越慢。貌似他run之前都会把所有项目都试图更新一下。勇哥宿舍的电脑,实...

C# 查询运算符测试

C# 查询运算符测试

测试一下全部的查询运算符。每天测试一部分,代码会持续更新中……C#using System;using System.Collections;using System.Collections.Generic;using System.Linq;using&nb...

C# 异步和等待,async/await

C# 异步和等待,async/await

 首先,重点是:异步将从根本上改变大多数代码的编写方式。是的,我相信 async/await 会比 LINQ 产生更大的影响。理解异步将在短短几年内成为基本必需品。关键字介绍让我们直接开始吧。我将使用一些稍后将阐述的概念——请继续阅读第一部分。异步方法看起来像这样:public ...

C# .Net 多进程同步 通信 共享内存 内存映射文件 Memory Mapped

C# .Net 多进程同步 通信 共享内存 内存映射文件 Memory Mapped

 节点通信存在两种模型:共享内存(Shared memory)和消息传递(Messages passing)。        内存映射文件对于托管世界的开发人员来说似乎很陌生,但它确实已经是很远古的技术了,而且在操作系统...

发表评论

访客

◎欢迎参与讨论,请在这里发表您的看法和观点。