轉(zhuǎn)帖|使用教程|編輯:龔雪|2022-06-17 11:27:33.637|閱讀 315 次
概述:本文主要介紹如何在Winform開(kāi)發(fā)框架中下拉列表綁定字典以及使用緩存提高界面顯示速度,歡迎下載推薦產(chǎn)品體驗(yàn)!
# 界面/圖表報(bào)表/文檔/IDE等千款熱門(mén)軟控件火熱銷(xiāo)售中 >>
相關(guān)鏈接:
前面介紹了通過(guò)擴(kuò)展函數(shù)來(lái)進(jìn)一步擴(kuò)展我們綁定字典類(lèi)別的方式了,如下擴(kuò)展函數(shù)所示。
/// <summary> /// 綁定下拉列表控件為指定的數(shù)據(jù)字典列表 /// </summary> /// <param name="combo">下拉列表控件</param> /// <param name="dictTypeName">數(shù)據(jù)字典類(lèi)型名稱(chēng)</param> /// <param name="defaultValue">控件默認(rèn)值</param> public static void BindDictItems(this ComboBoxEdit combo, string dictTypeName, string defaultValue) { Dictionary<string, string> dict = BLLFactory<DictData>.Instance.GetDictByDictType(dictTypeName); List<CListItem> itemList = new List<CListItem>(); foreach (string key in dict.Keys) { itemList.Add(new CListItem(key, dict[key])); } BindDictItems(combo, itemList, defaultValue); }
如果是基于服務(wù)接口的方式(通過(guò)Web API或者WCF方式)獲取字典列表,那么BLLFactory<T>的方式就修改為CallerFactory<T>的方式獲取數(shù)據(jù)了,如下擴(kuò)展函數(shù)所示。
/// <summary> /// 綁定下拉列表控件為指定的數(shù)據(jù)字典列表 /// </summary> /// <param name="control">下拉列表控件</param> /// <param name="dictTypeName">數(shù)據(jù)字典類(lèi)型名稱(chēng)</param> /// <param name="defaultValue">控件默認(rèn)值</param> /// <param name="emptyFlag">是否添加空行</param> public static void BindDictItems(this ComboBoxEdit control, string dictTypeName, string defaultValue, bool emptyFlag = true) { Dictionary<string, string> dict = CallerFactory<IDictDataService>.Instance.GetDictByDictType(dictTypeName); List<CListItem> itemList = new List<CListItem>(); foreach (string key in dict.Keys) { itemList.Add(new CListItem(key, dict[key])); } control.BindDictItems(itemList, defaultValue, emptyFlag); }
也就是通過(guò)服務(wù)接口工廠方法調(diào)用:
CallerFactory<IDictDataService>.Instance.GetDictByDictType(dictTypeName);
而獲取數(shù)據(jù)字典列表的內(nèi)容,這個(gè)可以配置為Web API訪問(wèn)方式、WCF訪問(wèn)方式,底層就是調(diào)用客戶(hù)端封裝的代理方法獲取就是了。例如對(duì)于Web API調(diào)用來(lái)說(shuō)就是通過(guò)客戶(hù)端直接訪問(wèn)Web API服務(wù)接口獲取數(shù)據(jù)的,實(shí)現(xiàn)代碼如下所示。
/// <summary> /// 根據(jù)字典類(lèi)型名稱(chēng)獲取所有該類(lèi)型的字典列表集合(Key為名稱(chēng),Value為值) /// </summary> /// <param name="dictTypeName">字典類(lèi)型名稱(chēng)</param> /// <returns></returns> public Dictionary<string, string> GetDictByDictType(string dictTypeName) { var action = System.Reflection.MethodBase.GetCurrentMethod().Name; string url = GetTokenUrl(action) + string.Format("&dictTypeName={0}", dictTypeName.UrlEncode()); Dictionary<string, string> result = JsonHelper<Dictionary<string, string>>.ConvertJson(url); return result; }
由于字典數(shù)據(jù)是相對(duì)比較固定的,一般時(shí)效不是那么及時(shí)都沒(méi)問(wèn)題,由于這部分?jǐn)?shù)據(jù)是通過(guò)網(wǎng)絡(luò)的方式獲取的,反復(fù)的調(diào)用獲取是會(huì)耗費(fèi)一定的時(shí)間。
為了提高用戶(hù)響應(yīng)速度,我們可以把它放到客戶(hù)端的緩存里面(非服務(wù)器緩存),設(shè)置一定的失效時(shí)間,在失效時(shí)間內(nèi),我們數(shù)據(jù)不再反復(fù)的從網(wǎng)絡(luò)接口獲取,而是直接通過(guò)緩存里面提取,速度非常快,同時(shí)也提高了界面響應(yīng)速度。
但是為了不影響已有代碼,我們可以繼續(xù)在擴(kuò)展函數(shù)的實(shí)現(xiàn)上做一些擴(kuò)展即可,首先我們定義一個(gè)公共的獲取字典數(shù)據(jù)的方法,如下所示。
/// <summary> /// 獲取字典類(lèi)型的通用處理 /// </summary> /// <param name="dictTypeName">字典類(lèi)型</param> /// <param name="isCache">是否緩存,默認(rèn)為true</param> /// <returns></returns> private static Dictionary<string, string> GetDictByDictType(string dictTypeName, bool isCache = true) { Dictionary<string, string> dict = null; if (isCache) { System.Reflection.MethodBase method = System.Reflection.MethodBase.GetCurrentMethod(); string key = string.Format("{0}-{1}-{2}", method.DeclaringType.FullName, method.Name, dictTypeName); dict = MemoryCacheHelper.GetCacheItem<Dictionary<string, string>>(key, delegate () { return CallerFactory<IDictDataService>.Instance.GetDictByDictType(dictTypeName); }, new TimeSpan(0, 30, 0));//30分鐘過(guò)期 } else { dict = CallerFactory<IDictDataService>.Instance.GetDictByDictType(dictTypeName); } return dict; }
通過(guò)使用 MemoryCacheHelper.GetCacheItem<Dictionary<string, string>> 的方式,我們可以把它設(shè)置為緩存處理方式,如果在失效時(shí)間內(nèi),則從緩存里面提取。
這樣原來(lái)的綁定下拉列表的擴(kuò)展方法獲取字典數(shù)據(jù),從這個(gè)公共的接口里面獲取即可,而我們也僅僅是增加一個(gè)具有默認(rèn)值的緩存與否的參數(shù),用來(lái)決定是否使用緩存模式,默認(rèn)為使用緩存處理。
/// <summary> /// 綁定下拉列表控件為指定的數(shù)據(jù)字典列表 /// </summary> /// <param name="control">下拉列表控件</param> /// <param name="dictTypeName">數(shù)據(jù)字典類(lèi)型名稱(chēng)</param> /// <param name="defaultValue">控件默認(rèn)值</param> /// <param name="emptyFlag">是否添加空行</param> public static void BindDictItems(this ComboBoxEdit control, string dictTypeName, string defaultValue, bool isCache = true, bool emptyFlag = true) { var dict = GetDictByDictType(dictTypeName, isCache); List<CListItem> itemList = new List<CListItem>(); foreach (string key in dict.Keys) { itemList.Add(new CListItem(key, dict[key])); } control.BindDictItems(itemList, defaultValue, emptyFlag); }
這樣原來(lái)的數(shù)據(jù)下拉列表綁定的方式?jīng)]有變化,依舊是我們?cè)瓉?lái)的代碼,但是默認(rèn)采用緩存方式來(lái)綁定基于網(wǎng)絡(luò)接口(混合框架模式)獲取的字典數(shù)據(jù)。
/// <summary> /// 初始化數(shù)據(jù)字典 /// </summary> private void InitDictItem() { //初始化代碼 this.txtSurgeryType.BindDictItems("手術(shù)方式"); this.txtIsFirstTime.BindDictItems("首發(fā)"); this.txtWHOGrade.BindDictItems("病理WHO分級(jí)"); this.txtLesionPart.BindDictItems("病灶部位"); this.txtOccupation.BindDictItems("病人職業(yè)"); this.txtRelapse.BindDictItems("復(fù)發(fā)"); this.txtPathologyGrade.BindDictItems("病理分級(jí)"); this.txtSymptom.BindDictItems("初發(fā)癥狀"); this.txtAnesthesiaMethod.BindDictItems("麻醉方法"); this.txtSpecimenDetail.BindDictItems("具體標(biāo)本情況"); }
得到的編輯界面如下所示,使用緩存接口,對(duì)于大量字典數(shù)據(jù)顯示的界面,界面顯示速度有了不錯(cuò)的提升。
而對(duì)于一些特殊列表的字典顯示,如需要通過(guò)拼音首字母進(jìn)行檢索功能的下拉列表,我們依舊可以使用這種綁定的方式實(shí)現(xiàn)緩存處理的。
如字典綁定的擴(kuò)展函數(shù)如下所示,這樣就統(tǒng)一了整個(gè)字典列表的綁定操作,比較容易記住。
/// <summary> /// 綁定下拉列表控件為指定的數(shù)據(jù)字典列表 /// </summary> /// <param name="combo">下拉列表控件</param> /// <param name="dictTypeName">數(shù)據(jù)字典類(lèi)型名稱(chēng)</param> /// <param name="defaultValue">控件默認(rèn)值</param> public static void BindDictItems(this CustomGridLookUpEdit combo, string dictTypeName, string defaultValue, bool isCache = true) { string displayName = dictTypeName; const string valueName = "值內(nèi)容"; const string pinyin = "拼音碼"; var dt = DataTableHelper.CreateTable(string.Format("{0},{1},{2}", displayName, valueName, pinyin)); var dict = GetDictByDictType(dictTypeName, isCache); foreach (string key in dict.Keys) { var row = dt.NewRow(); row[displayName] = key; row[valueName] = dict[key]; row[pinyin] = Pinyin.GetFirstPY(key); dt.Rows.Add(row); } combo.Properties.ValueMember = valueName; combo.Properties.DisplayMember = displayName; combo.Properties.DataSource = dt; combo.Properties.PopulateViewColumns(); combo.Properties.View.Columns[valueName].Visible = false; combo.Properties.View.Columns[displayName].Width = 400; combo.Properties.View.Columns[pinyin].Width = 200; combo.Properties.PopupFormMinSize = new System.Drawing.Size(600, 0); if (!string.IsNullOrEmpty(defaultValue)) { combo.EditValue = defaultValue; } }
界面效果如下所示。
以上就是常規(guī)單機(jī)版數(shù)據(jù)綁定操作,以及基于網(wǎng)絡(luò)版緩存數(shù)據(jù)的數(shù)據(jù)字典綁定操作,我們?cè)诮缑娲a的處理上沒(méi)有任何差異,只是輔助擴(kuò)展函數(shù)做一些調(diào)整就可以很好的變化過(guò)來(lái)了,這樣對(duì)于我們界面代碼的重用或者調(diào)整是非常便利的,同時(shí)緩存的使用,對(duì)于網(wǎng)絡(luò)性能有所差異的地方,速度也會(huì)明細(xì)的有所提高。以上就是對(duì)于字典模塊的一些處理上的分享,希望對(duì)大家開(kāi)發(fā)Winform界面代碼有所幫助和啟發(fā)。
DevExpress WinForm擁有180+組件和UI庫(kù),能為Windows Forms平臺(tái)創(chuàng)建具有影響力的業(yè)務(wù)解決方案。DevExpress WinForms能完美構(gòu)建流暢、美觀且易于使用的應(yīng)用程序,無(wú)論是Office風(fēng)格的界面,還是分析處理大批量的業(yè)務(wù)數(shù)據(jù),它都能輕松勝任!
本文轉(zhuǎn)載自:
DevExpress技術(shù)交流群6:600715373 歡迎一起進(jìn)群討論
本站文章除注明轉(zhuǎn)載外,均為本站原創(chuàng)或翻譯。歡迎任何形式的轉(zhuǎn)載,但請(qǐng)務(wù)必注明出處、不得修改原文相關(guān)鏈接,如果存在內(nèi)容上的異議請(qǐng)郵件反饋至chenjj@ke049m.cn
文章轉(zhuǎn)載自:慧都網(wǎng)