当前位置:首页 > Windows程序 > 正文

MVC in WinForm

2021-03-25 Windows程序

最近在利用C# WinForm进行项目编码的时候发现,如果不恰当的使用Partial关键字或者是User Control,会导致逻辑代码和UI代码耦合度过高,往往会造成一个页面下面会有将近2000行代码……这,当然不是我所要看到的,我急需要一种模式能够将代码分散,并且最好做到逻辑和UI的分离。找来找去,发现MVC模式最好用,就用这个吧。

当然,拿来用,,肯定得明白它的实现方式,对于MVC这种模式,我也已经不是第一次接触了,就来简单的说点吧。

MVC基本概念

MVC,指的是Model View Controller,其核心是利用Controller来策动Model和View,简而言之,Controller就是生成Model和View的。请看下面的图示:

这里我就来说下创建步骤:

首先,Controller中需要有待操作的View和与之对应的Model,然后主要进行逻辑代码的编写。

其次,View中需要有之对应的Controller的实例,这样才能方便调用Controller中的逻辑方法。

最后就是启动的时候,最好是Controller First,而不是View First.

需要注意的是,在MVC中,我们倾向于在Model类中使用INtofifyPropertyChanged接口;我们倾向于利用DataBindings来绑定数据。

MVC具体使用实例

下面就以一个刚刚编写的字模制作小软件来说明:

在Controller中,我们需要指明需要操作的View和Model:

public WordMakerController(MainFrm mainFrm)
{
this.mainFrm = mainFrm;
this.mainFrm.MController = this;

//调用构造器实现数据缓存
wordReponsitory = new WordReponsitory();
}

//我要操作的View
public MainFrm mainFrm;

//我要操作的Model
public object datasource = DataCache.model;

指明之后,剩下的就是操作逻辑了:

//添加操作,主要是将标记写入
public void Add(Button thisBtn, string flag)
{

thisBtn.Text = flag;
string content = string.Empty;
if (flag.Contains("http://www.mamicode.com/"))
content = @"strArray[" + thisBtn.Tag + "]=" + "\"/\";";
if (flag.Contains("\\"))
content = @"strArray[" + thisBtn.Tag + "]=" + "@" + "\"\\\";";

if (!list.Contains(content))
{
list.Add(content);
SortAndIntegration();
}
}

//删除操作,删除已有的标记。
public void Remove(Button thisBtn, string flag)
{
string content = string.Empty;
if (flag.Contains("http://www.mamicode.com/"))
content = @"strArray[" + thisBtn.Tag + "]=" + "\"/\";";
if (flag.Contains("\\"))
content = @"strArray[" + thisBtn.Tag + "]=" + "@" + "\"\\\";";

thisBtn.Text = string.Empty;
if (list.Contains(content))
{
list.Remove(content);
SortAndIntegration();
}
}

//对List数组进行排序
private void SortAndIntegration()
{
list.Sort(1, list.Count - 1, null);
DataCache.model.MyArray = string.Join(newLine, list.ToArray());
}

//本字模软件的核心就是此类,将输入的字母拷贝到一个大数组,然后循环行列打印出来。
private void Punch(int count, string[,] strDaemon, string[,] strPartly)
{
for (int row = 0; row < 7; row++)
{
for (int column = 0; column < 16; column++)
{
strDaemon[row, count * 16 + column] = strPartly[row, column];
}
}
}

//解析输入的单词
public string[,] make(string text)
{
//string text = txtWord.Text;

int len = text.Length;

string[,] str = new string[7, len * 16];
int count = -1;
foreach (char c in text)
{
switch (c)
{
case ‘A‘:
count++;
Punch(count, str, WordReponsitory.strArrayA);
break;

case ‘B‘:
count++;
Punch(count, str, WordReponsitory.strArrayB);
break;

case ‘C‘:
count++;
Punch(count, str, WordReponsitory.strArrayC);
break;

case ‘D‘:
count++;
Punch(count, str, WordReponsitory.strArrayD);
break;

case ‘E‘:
count++;
Punch(count, str, WordReponsitory.strArrayE);
break;

case ‘F‘:
count++;
Punch(count, str, WordReponsitory.strArrayF);
break;

case ‘G‘:
count++;
Punch(count, str, WordReponsitory.strArrayG);
break;

case ‘H‘:
count++;
Punch(count, str, WordReponsitory.strArrayH);
break;

case ‘I‘:
count++;
Punch(count, str, WordReponsitory.strArrayI);
break;

case ‘J‘:
count++;
Punch(count, str, WordReponsitory.strArrayJ);
break;

case ‘K‘:
count++;
Punch(count, str, WordReponsitory.strArrayK);
break;

case ‘L‘:
count++;
Punch(count, str, WordReponsitory.strArrayL);
break;

case ‘T‘:
count++;
Punch(count, str, WordReponsitory.strArrayT);
break;

case ‘O‘:
count++;
Punch(count, str, WordReponsitory.strArrayO);
break;
default: break;


}
}
return str;
}

在Model中,倾向于利用INotifyPropertyChanged接口实现属性改变后通知到绑定

public class NotifiedModel:INotifyPropertyChanged
{

private string myArray;
public string MyArray
{
get { return myArray; }
set { myArray = value; NotifyPropertyChanged("MyArray"); }
}

public event PropertyChangedEventHandler PropertyChanged;

温馨提示: 本文由Jm博客推荐,转载请保留链接: https://www.jmwww.net/file/66954.html