在這裡我也就不再介紹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