技术标签: C# winform mvc 设计框架 设计模式
MVC模式主要解决的问题就是将表示层和业务层进行分离,在以往做WINFORM项目的时候,通常都是将很多的逻辑代码直接写在了Form.cs代码的事件里,这样的话业务逻辑就和界面紧耦合在一起了,现在我们采用MVC来解耦。
首先建立Model:
[csharp] view plain copy
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的实现:
[csharp] view plain copy
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);
}
//这边的业务逻辑应该写在model里比较合适
}
}
然后是View的实现:
[csharp] view plain copy
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)
{
}
}
}
最后是程序启动:
[csharp] view plain copy
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);
}
}
}
文章浏览阅读100次。#11-1city_functions.pydef city_country(city, country): return (city + ", " + country).title()test_cities.pyimport unittestfrom city_functions.py import city_countryclass CitysTestCase(u..._python第6周
文章浏览阅读66次。安装相应的驱动程序后,现在是时候来学习使用JDBC建立数据库连接了。建立JDBC连接所涉及的编程相当简单。 以下是基本的四个步骤 -导入JDBC包:使用Java语言的import语句在Java代码开头位置导入所需的类。注册JDBC驱动程序:使JVM将所需的驱动程序实现加载到内存中,从而可以满足JDBC请求。数据库URL配置:创建一个正确格式化的地址,指向要连接到的数据库(如:MySQ..._可以通过两种方式注册数据库驱动程序,一是使用( )方法,二是使用( )方法。
文章浏览阅读2.6k次。最近看源码就想找个uml的类图工具,网上看了一些,发现都是一些单个类的继承关系图,如果不知道的朋友,可以用uml插件工具搜索uml即可(uml support),然后每次点击右键后在菜单下面找Diagrams,但是我们今天介绍的不是这个,而是另外一个不错的插件,叫code iris。这个是可以显示整个工程所有类关系图的插件。插件中心查找我的是已经安装过了,没有安装的可以浏览所有的,并查找用法:这个工具用法比较独特,在点击右键中找不到对应的菜单进行操作。而是在右边有一个菜单,如下._code iris
文章浏览阅读3.5k次,点赞3次,收藏10次。前言:作为一个前端,怎么能仅仅只会写代码,然后打包代码给后端部署呢?不!咱要自立自强!本篇文章为笔者实践过程的笔记,如果有小伙伴跟我一样是服务器小白,可以作为参考借鉴,同时,如果有错误之处,欢迎各位大佬指正。使用背景:腾讯云轻量应用服务器,镜像为CentOS 7.6 64bit,应用镜像为宝塔linux面板 7.6.0。开始攻克!!!第一步:安装宝塔面板步骤:【概要】→【镜像信息】→【重置应用】。按照图片标识的步骤即可。安装成功:可在【概要】→【镜像信息】查看。获取宝塔登录的账号以及密码:_腾讯宝塔tomcat配置域名
文章浏览阅读1.1k次。看了个算法题目,觉得有趣,就换成了java版本的。 原文地址:blog.csdn.net/ns_code/article/details/21409663 题目: Write a method to decide if two strings are anagrams or..._互为变位词
文章浏览阅读989次。localhost:proj.android mxhd4$ ./build_native.sh NDK_ROOT = /Users/mxhd4/Movies/android-ndk-r9cCOCOS2DX_ROOT = /Users/mxhd4/Movies/2.0.4/cocos2d-2.0-x-2.0.4/test_cocos2dx_mac/proj.android/../..AP
文章浏览阅读887次。环境:Visual Studio 2010, C#;前言:本来是用VS2010连接Oracle数据库做一个报表功能,数据集设置部分出了些问题还未解决,因此先用本地动态数据为测试用例先熟悉VS自带的ReportViewer控件。新手上路,共同进步。牛蛙可以忽略。本文主要包含以下内容:一、ReportViewer使用小例子的完整步骤(新建-设计-编码-调试-结果);二、ReportVi..._visual studio reportviewer 教程
文章浏览阅读1.1k次。题目链接 蓝桥杯 算法训练---------题解锦囊1枚举加二分答案。锦囊2先用枚举初步确定三个根的范围,比如f[i]*f[i+1]<0则可知道[i,i+1]之间有一个根,然后再对于每个范围内二分求根。问题描述 有形如:ax3+bx2+cx+d=0 这样的一个一元三次方程。给出该方程中各项的系数(a,b..._算法设计与分析求解一元三次方程
文章浏览阅读1.1w次,点赞2次,收藏13次。WordPress网站开放投稿功能,接受读者的投稿。但WordPress本身并不提供投稿功能,只拥有强大的扩展能力,我们可以自己添加这个投稿功能。实现用户投稿,有两种方法:一种是开放后台注册功能,普通用户注册进去默认设置为投稿者,登陆进去即可添加文章(默认为草稿);另一种是在前台提供投稿表单,用户填写相应的表格,例如米扑博客:http://blog.mimvp.com前一种方法实现起来比较简单,基_wordpress投稿
文章浏览阅读1.5k次。将项目打包好并上传到服务器,打开服务器命令行界面,cd到项目包目录下,debug模式运行项目java -Xdebug -Xrunjdwp:server=y,transport=dt_socket,address=8000,suspend=n -jar 项目包运行成功后,去到Eclipse点击小强,Debug Configurations–>Remote Java Applicati...
文章浏览阅读205次。量子力学已经是现代物理学的基础学科之一,其影响力越来越大!巨大的影响力迫使着人们了解它,可量子世界中的种种奇异现象却挑战着常人的逻辑底线。甚至 让许多物理爱好者也摸不着头脑, 以至于玻尔(量子物理学家)说到“如果一个人第一次听到量子物理而不感到困惑,那他一定是没有听懂”!薛定谔薛定谔的猫 只是帮助人们理解量子世界 的一种思想实验!薛定谔的猫是193...
文章浏览阅读172次。gitub网址:https://github.com/Bilibili/ijkplayer1.编译环境 ruby -e “$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)” apt-get install git apt-get install yasm2.设置Linux环境变量_jjdxm_ijkplayer直播