在这里我也就不再介绍DataUIMapper组件的详细功能了,直接看看改造过程,我们先来看看原先的显示与写回代码:
1 private void DataDisplay_Old(IProduct dict)
2 {
3 this.tbCode.Text = dict.Code;
4 this.tbName.Text = dict.Name;
5 this.tbSpec.Text = dict.Spec;
6 this.tbUnit.Text = dict.Unit;
7 this.tbDescription.Text = dict.Description;
8 this.tbPYCode.Text = dict.PYCode;
9 }
10
11 private void WriteBack_Old(IProduct dict)
12 {
13 dict.Code = this.tbCode.Text;
14 dict.Name = this.tbName.Text;
15 dict.Spec = this.tbSpec.Text;
16 dict.Unit = this.tbUnit.Text;
17 dict.Description = this.tbDescription.Text;
18 dict.PYCode = this.tbPYCode.Text;
19 }
原来的代码需要手工实现UI控件与数据对象的交互比如dict.Code = this.tbCode.Text这样的写法,那么换成DataUIMapper组件应该怎么做呢。
首先在VS开发环境中把DataUIMapper组件添加到VS环境的工具箱,需要说明的是DataUIMapper组件由程序集EAS.Data.DataUIMapper.dll承载,然后向窗体ProductDictEditor拖放一个DataUIMapper组件,然后设定DataUIMapper组件的数据源属性:
<a href="http://images.cnblogs.com/cnblogs_com/eastjade/WindowsLiveWriter/AgileEAS.NETWinFormUIDataMapper_C6E9/image_2.png"></a>
DataUIMapper组件可以通过BindingSource数据实现向数据对象的绑定,也就是可以属性窗口中很方便的设定数据源,当然,开发人员也可以通过其他方式设定数据源,比如可以通过修改ProductDictEditor.Designer.cs文件指定DataUIMapper组件的数据源对象:
<a href="http://images.cnblogs.com/cnblogs_com/eastjade/WindowsLiveWriter/AgileEAS.NETWinFormUIDataMapper_C6E9/1Z(IP%7B5%7DFVZ7ZLN%24%7BCFJ%5DTH_2.jpg"></a>
虽然可以通过修改*.Designer.cs,但我还是建议通过GUI文件完成DataUIMapper组件的数据源设定,当完成DataUIMapper组件的数据源设定之后,我们就可以使用很方便的Data<==>UI绑定了,可以通过DataUIMapper组件的Mappings属性(集合)来配置数据对象与UI控件的交互关系:
<a href="http://images.cnblogs.com/cnblogs_com/eastjade/WindowsLiveWriter/AgileEAS.NETWinFormUIDataMapper_C6E9/image_6.png"></a>
<a href="http://images.cnblogs.com/cnblogs_com/eastjade/WindowsLiveWriter/AgileEAS.NETWinForm_C704/image_6.png"></a>
在属性设定界面上有一个选项,“常用/全部”,选择“常用”之后控件下拉列表中会把一些不常用的UI控件过虑,但如果使用了一些特殊控件,被考虑了你需要选择“全部”,则可以完成对UI中的所有控件进行设定。
当我们完成绑定Data<==>UI绑定关系之后,我们只需要修改原先的数据显示与写回代码如下即可:
1 private void DataDisplay(IProduct dict)
2 {
3 this.dataUIMapper1.UpdateUI(dict);
4 }
5
6 private void WriteBack(IProduct dict)
7 {
8 this.dataUIMapper1.UpdateObject(dict);
9 }
最后,声明一句,不欢迎“巴克球”!我不想再说什么,别来凑热闹了。
链接
<a href="http://www.cnblogs.com/eastjade/archive/2010/09/19/1830812.html">一步一步教你使用AgileEAS.NET基础类库进行应用开发-系列目录</a>
<a href="http://www.cnblogs.com/eastjade/archive/2010/09/12/1824405.html">AgileEAS.NET平台开发指南-系列目录</a>
<a href="http://www.cnblogs.com/eastjade/archive/2010/09/09/1822530.html">AgileEAS.NET应用开发平台介绍-文章索引</a>
<a href="http://www.cnblogs.com/eastjade/archive/2010/09/15/1826870.html">AgileEAS.NET平台应用开发教程-案例计划</a>
<a href="http://www.smarteas.net/">AgileEAS.NET官方网站</a>
<a href="http://www.agilelab.cn/">敏捷软件工程实验室</a>
QQ群:116773358
本文转自 agilelab 51CTO博客,原文链接:http://blog.51cto.com/agilelab/603045