轉帖|使用教程|編輯:龔雪|2022-06-17 11:27:33.637|閱讀 303 次
概述:本文主要介紹如何在Winform開發框架中下拉列表綁定字典以及使用緩存提高界面顯示速度,歡迎下載推薦產品體驗!
# 界面/圖表報表/文檔/IDE等千款熱門軟控件火熱銷售中 >>
相關鏈接:
前面介紹了通過擴展函數來進一步擴展我們綁定字典類別的方式了,如下擴展函數所示。
/// <summary> /// 綁定下拉列表控件為指定的數據字典列表 /// </summary> /// <param name="combo">下拉列表控件</param> /// <param name="dictTypeName">數據字典類型名稱</param> /// <param name="defaultValue">控件默認值</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); }
如果是基于服務接口的方式(通過Web API或者WCF方式)獲取字典列表,那么BLLFactory<T>的方式就修改為CallerFactory<T>的方式獲取數據了,如下擴展函數所示。
/// <summary> /// 綁定下拉列表控件為指定的數據字典列表 /// </summary> /// <param name="control">下拉列表控件</param> /// <param name="dictTypeName">數據字典類型名稱</param> /// <param name="defaultValue">控件默認值</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); }
也就是通過服務接口工廠方法調用:
CallerFactory<IDictDataService>.Instance.GetDictByDictType(dictTypeName);
而獲取數據字典列表的內容,這個可以配置為Web API訪問方式、WCF訪問方式,底層就是調用客戶端封裝的代理方法獲取就是了。例如對于Web API調用來說就是通過客戶端直接訪問Web API服務接口獲取數據的,實現代碼如下所示。
/// <summary> /// 根據字典類型名稱獲取所有該類型的字典列表集合(Key為名稱,Value為值) /// </summary> /// <param name="dictTypeName">字典類型名稱</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; }
由于字典數據是相對比較固定的,一般時效不是那么及時都沒問題,由于這部分數據是通過網絡的方式獲取的,反復的調用獲取是會耗費一定的時間。
為了提高用戶響應速度,我們可以把它放到客戶端的緩存里面(非服務器緩存),設置一定的失效時間,在失效時間內,我們數據不再反復的從網絡接口獲取,而是直接通過緩存里面提取,速度非常快,同時也提高了界面響應速度。
但是為了不影響已有代碼,我們可以繼續在擴展函數的實現上做一些擴展即可,首先我們定義一個公共的獲取字典數據的方法,如下所示。
/// <summary> /// 獲取字典類型的通用處理 /// </summary> /// <param name="dictTypeName">字典類型</param> /// <param name="isCache">是否緩存,默認為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分鐘過期 } else { dict = CallerFactory<IDictDataService>.Instance.GetDictByDictType(dictTypeName); } return dict; }
通過使用 MemoryCacheHelper.GetCacheItem<Dictionary<string, string>> 的方式,我們可以把它設置為緩存處理方式,如果在失效時間內,則從緩存里面提取。
這樣原來的綁定下拉列表的擴展方法獲取字典數據,從這個公共的接口里面獲取即可,而我們也僅僅是增加一個具有默認值的緩存與否的參數,用來決定是否使用緩存模式,默認為使用緩存處理。
/// <summary> /// 綁定下拉列表控件為指定的數據字典列表 /// </summary> /// <param name="control">下拉列表控件</param> /// <param name="dictTypeName">數據字典類型名稱</param> /// <param name="defaultValue">控件默認值</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); }
這樣原來的數據下拉列表綁定的方式沒有變化,依舊是我們原來的代碼,但是默認采用緩存方式來綁定基于網絡接口(混合框架模式)獲取的字典數據。
/// <summary> /// 初始化數據字典 /// </summary> private void InitDictItem() { //初始化代碼 this.txtSurgeryType.BindDictItems("手術方式"); this.txtIsFirstTime.BindDictItems("首發"); this.txtWHOGrade.BindDictItems("病理WHO分級"); this.txtLesionPart.BindDictItems("病灶部位"); this.txtOccupation.BindDictItems("病人職業"); this.txtRelapse.BindDictItems("復發"); this.txtPathologyGrade.BindDictItems("病理分級"); this.txtSymptom.BindDictItems("初發癥狀"); this.txtAnesthesiaMethod.BindDictItems("麻醉方法"); this.txtSpecimenDetail.BindDictItems("具體標本情況"); }
得到的編輯界面如下所示,使用緩存接口,對于大量字典數據顯示的界面,界面顯示速度有了不錯的提升。
而對于一些特殊列表的字典顯示,如需要通過拼音首字母進行檢索功能的下拉列表,我們依舊可以使用這種綁定的方式實現緩存處理的。
如字典綁定的擴展函數如下所示,這樣就統一了整個字典列表的綁定操作,比較容易記住。
/// <summary> /// 綁定下拉列表控件為指定的數據字典列表 /// </summary> /// <param name="combo">下拉列表控件</param> /// <param name="dictTypeName">數據字典類型名稱</param> /// <param name="defaultValue">控件默認值</param> public static void BindDictItems(this CustomGridLookUpEdit combo, string dictTypeName, string defaultValue, bool isCache = true) { string displayName = dictTypeName; const string valueName = "值內容"; 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; } }
界面效果如下所示。
以上就是常規單機版數據綁定操作,以及基于網絡版緩存數據的數據字典綁定操作,我們在界面代碼的處理上沒有任何差異,只是輔助擴展函數做一些調整就可以很好的變化過來了,這樣對于我們界面代碼的重用或者調整是非常便利的,同時緩存的使用,對于網絡性能有所差異的地方,速度也會明細的有所提高。以上就是對于字典模塊的一些處理上的分享,希望對大家開發Winform界面代碼有所幫助和啟發。
DevExpress WinForm擁有180+組件和UI庫,能為Windows Forms平臺創建具有影響力的業務解決方案。DevExpress WinForms能完美構建流暢、美觀且易于使用的應用程序,無論是Office風格的界面,還是分析處理大批量的業務數據,它都能輕松勝任!
本文轉載自:
DevExpress技術交流群6:600715373 歡迎一起進群討論
本站文章除注明轉載外,均為本站原創或翻譯。歡迎任何形式的轉載,但請務必注明出處、不得修改原文相關鏈接,如果存在內容上的異議請郵件反饋至chenjj@fc6vip.cn
文章轉載自:慧都網