/// <summary> /// /// </summary> /// <param name="title">要顯示的標題文字。</param> /// <param name="fieldName">對應資料庫或是 Value Object 的欄位名稱。</param> public ListPaneFieldImproved(string title, string fieldName) { FieldName = fieldName; CacheProvider = null; CurrentCache = null; if (Backend == null) { Backend = new QueryHelper(); } Field = new ListPaneField(title); Field.CompareValue += (CompareValue); Field.PreloadVariable += (Field_PreloadVariable); Field.GetVariable += (Field_GetVariable); UISyncContext = TaskScheduler.FromCurrentSynchronizationContext(); }
private void Field_PreloadVariable(object sender, PreloadVariableEventArgs e) { if (IsDataLoading) { IsPaddingTask = true; return; } if (IsReloading) { return; } if (CacheProvider != null) { CurrentCache = CacheProvider; } else { CurrentCache = new DynamicCache(); } IsDataLoading = true; Task task = Task.Factory.StartNew(() => { if (CurrentCache.GetOutOfDate(e.Keys, FieldName)) {//如果不是在最新狀態就呼叫 GetDataAsync 讀取資料,並更新到 Cache 中。 if (e.Keys.Count() <= 0) { return; //如果沒有資料就不執行。 } IEnumerable <Value> values = GetDataAsync(e.Keys); HashSet <string> resultSet = new HashSet <string>(); foreach (Value v in values) { CurrentCache.FillProperty(v.Id, FieldName, v.Val); resultSet.Add(v.Id); } HashSet <string> clearSet = new HashSet <string>(e.Keys); clearSet.ExceptWith(resultSet); //將沒有回傳的 Id 值清空。 foreach (string id in clearSet) { CurrentCache.FillProperty(id, FieldName, string.Empty); } } }, new CancellationToken(), TaskCreationOptions.PreferFairness, TaskScheduler.Default); task.ContinueWith((x) => { IsDataLoading = false; if (IsPaddingTask) { IsPaddingTask = false; Field.Reload(); return; } if (x.Exception != null) { RTOut.WriteError(x.Exception); } else { IsReloading = true; Field.Reload(); IsReloading = false; } CurrentCache = null; }, UISyncContext); }