UCVpsGrid.xaml.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407
  1. using MeterVision.Config;
  2. using MeterVision.db;
  3. using MeterVision.Helper;
  4. using MeterVision.model;
  5. using MeterVision.Util;
  6. using System;
  7. using System.Collections.Generic;
  8. using System.Collections.ObjectModel;
  9. using System.ComponentModel;
  10. using System.Linq;
  11. using System.Text;
  12. using System.Threading;
  13. using System.Threading.Tasks;
  14. using System.Windows;
  15. using System.Windows.Controls;
  16. using System.Windows.Data;
  17. using System.Windows.Documents;
  18. using System.Windows.Input;
  19. using System.Windows.Media;
  20. using System.Windows.Media.Imaging;
  21. using System.Windows.Navigation;
  22. using System.Windows.Shapes;
  23. namespace MeterVision.Station
  24. {
  25. /// <summary>
  26. /// UCVpsGrid.xaml 的交互逻辑
  27. /// </summary>
  28. public partial class UCVpsGrid : UserControl, INotifyPropertyChanged
  29. {
  30. public event PropertyChangedEventHandler PropertyChanged;
  31. protected void OnPropertyChanged(string propertyName)
  32. {
  33. PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
  34. }
  35. private SemaphoreSlim _semaphore = new SemaphoreSlim(1, 1);
  36. public BulkObservableCollection<VPatchStation> StationItemList { get; set; }
  37. public event Action<VPatchStation> OnItemChanged;
  38. private VPatchStation _selectedStationItem;
  39. public VPatchStation SelectedStationItem
  40. {
  41. get => _selectedStationItem;
  42. set
  43. {
  44. if(_selectedStationItem != value)
  45. {
  46. _selectedStationItem = value;
  47. OnPropertyChanged(nameof(SelectedStationItem));
  48. OnItemChanged?.Invoke(value);
  49. }
  50. }
  51. }
  52. //要查找的站点ID
  53. private string _findStationId;
  54. public string FindStationId
  55. {
  56. get => _findStationId;
  57. private set
  58. {
  59. if (_findStationId != value)
  60. {
  61. _findStationId = value;
  62. }
  63. }
  64. }
  65. private string _patchId;
  66. public string PatchId
  67. {
  68. get => _patchId;
  69. private set
  70. {
  71. if (_patchId != value)
  72. {
  73. _patchId = value;
  74. }
  75. }
  76. }
  77. public void ChangeFind(string stationId, string patchId)
  78. {
  79. //bool blChangeFindStationId = false;
  80. //bool blChangePatchId = false;
  81. if (!_semaphore.Wait(0)) return; // 如果信号量已被占用,直接返回
  82. try
  83. {
  84. if (stationId != _findStationId)
  85. {
  86. FindStationId = stationId;
  87. //blChangeFindStationId = true;
  88. }
  89. if (patchId != _patchId)
  90. {
  91. PatchId = patchId;
  92. // blChangePatchId = true;
  93. }
  94. //if (blChangeFindStationId || blChangePatchId)
  95. {
  96. StationPage.InitDefaulValue();
  97. LoadStationItemList(true);
  98. }
  99. }
  100. catch { }
  101. finally
  102. {
  103. _semaphore.Release();
  104. }
  105. }
  106. public CfginiItem mConfigItem { get; set; }
  107. public PageModel StationPage { get; set; }
  108. private int _totalRecords;
  109. public int TotalRecords
  110. {
  111. get => _totalRecords;
  112. set
  113. {
  114. if (_totalRecords != value)
  115. {
  116. _totalRecords = value;
  117. OnPropertyChanged(nameof(TotalRecords));
  118. }
  119. }
  120. }
  121. public UCVpsGrid()
  122. {
  123. InitializeComponent();
  124. StationItemList = new BulkObservableCollection<VPatchStation>();
  125. dgStation.ItemsSource = StationItemList;
  126. mConfigItem = CfginiItem.GetConfigItem();
  127. StationPage = new PageModel
  128. {
  129. PageSize = mConfigItem.PageSize2, //mConfigItem.PatchPageSize,
  130. PageNumber = 1
  131. };
  132. //mConfigItem.OnPatchPageSizeChanged += MConfigItem_OnPatchPageSizeChanged;
  133. mConfigItem.OnPageSize2Changed += MConfigItem_OnPageSize2Changed;
  134. this.DataContext = this;
  135. }
  136. private void MConfigItem_OnPageSize2Changed(int pageSize2)
  137. {
  138. if (!_semaphore.Wait(0)) return; // 如果信号量已被占用,直接返回
  139. try
  140. {
  141. StationPage.InitDefaulValue(pageSize2);
  142. LoadStationItemList(true);
  143. }
  144. catch { }
  145. finally
  146. {
  147. _semaphore.Release();
  148. }
  149. }
  150. //private void MConfigItem_OnPatchPageSizeChanged(int pageSize)
  151. //{
  152. // StationPage.InitDefaulValue(pageSize);
  153. // LoadStationItemList(true);
  154. //}
  155. private async void LoadStationItemList(bool scrollTop)
  156. {
  157. StationItemList.Clear();
  158. if (FindStationId == null)
  159. {
  160. return;
  161. }
  162. try
  163. {
  164. var result = await Task.Run(() =>
  165. {
  166. return DBPatch.GetPagedPatchStations(StationPage.PageNumber,StationPage.PageSize,FindStationId,PatchId);
  167. });
  168. //更新分页信息
  169. TotalRecords = result.Item1;
  170. StationPage.PageCount = result.Item2;
  171. List<VPatchStation> stationList = result.Item3;
  172. //在主线中更新数据
  173. //await Application.Current.Dispatcher.BeginInvoke(new Action(() =>
  174. //{
  175. //int index = 0;
  176. //foreach (VPatchStation station in stationList)
  177. //{
  178. // index++;
  179. // station.Index = index + ((StationPage.PageNumber - 1) * StationPage.PageSize);
  180. // StationItemList.Add(station);
  181. //}
  182. stationList = stationList.Select((station, i) =>
  183. {
  184. station.Index = i + 1 + ((StationPage.PageNumber - 1) * StationPage.PageSize); // 计算索引
  185. return station;
  186. }).ToList();
  187. StationItemList.AddRange(stationList);
  188. //SelectedStationItem = null;
  189. if (StationItemList.Count > 0 && scrollTop)
  190. {
  191. //dgStation.ScrollIntoView(StationItemList[0]);
  192. await Dispatcher.BeginInvoke(new Action(() =>
  193. {
  194. var scrollViewer = GetDgStationScrollViewer(dgStation);
  195. scrollViewer?.ScrollToTop();
  196. //dgStation.ScrollIntoView(dgStation.Items[0]);
  197. }));
  198. }
  199. }
  200. catch (Exception ex)
  201. {
  202. MessageBox.Show(Application.Current.MainWindow,
  203. $"F加载数据是发生错误:{ex.Message}", "错误",
  204. MessageBoxButton.OK, MessageBoxImage.Error);
  205. }
  206. //if(StationItemList.Count > 0)
  207. //{
  208. // SelectedStationItem = StationItemList[0];
  209. //}
  210. //else
  211. //{
  212. // SelectedStationItem = null;
  213. //}
  214. }
  215. public void NextPage()
  216. {
  217. if (!_semaphore.Wait(0)) return; // 如果信号量已被占用,直接返回
  218. try
  219. {
  220. if (StationPage.PageNumber < StationPage.PageCount)
  221. {
  222. StationPage.PageNumber += 1;
  223. LoadStationItemList(true);
  224. }
  225. }
  226. catch { }
  227. finally
  228. {
  229. _semaphore.Release();
  230. }
  231. }
  232. public void PrePage()
  233. {
  234. if (!_semaphore.Wait(0)) return; // 如果信号量已被占用,直接返回
  235. try
  236. {
  237. if (StationPage.PageNumber > 1)
  238. {
  239. StationPage.PageNumber -= 1;
  240. LoadStationItemList(true);
  241. }
  242. }
  243. catch { }
  244. finally
  245. {
  246. _semaphore.Release();
  247. }
  248. }
  249. public void FirstPage()
  250. {
  251. if (!_semaphore.Wait(0)) return; // 如果信号量已被占用,直接返回
  252. try
  253. {
  254. if (StationPage.PageNumber > 1)
  255. {
  256. StationPage.PageNumber = 1;
  257. LoadStationItemList(true);
  258. }
  259. }
  260. catch { }
  261. finally
  262. {
  263. _semaphore.Release();
  264. }
  265. }
  266. public void LastPage()
  267. {
  268. if (!_semaphore.Wait(0)) return; // 如果信号量已被占用,直接返回
  269. try
  270. {
  271. if (StationPage.PageNumber < StationPage.PageCount)
  272. {
  273. StationPage.PageNumber = StationPage.PageCount;
  274. LoadStationItemList(true);
  275. }
  276. }
  277. catch { }
  278. finally
  279. {
  280. _semaphore.Release();
  281. }
  282. }
  283. public void SpeciPage(int pageNumber)
  284. {
  285. if (!_semaphore.Wait(0)) return; // 如果信号量已被占用,直接返回
  286. try
  287. {
  288. if (pageNumber != StationPage.PageNumber &&
  289. pageNumber > 0 && pageNumber <= StationPage.PageCount)
  290. {
  291. StationPage.PageNumber = pageNumber;
  292. LoadStationItemList(true);
  293. }
  294. }
  295. catch { }
  296. finally
  297. {
  298. _semaphore.Release();
  299. }
  300. }
  301. public async void RefreshItems()
  302. {
  303. try
  304. {
  305. var result = await Task.Run(() =>
  306. {
  307. return DBPatch.GetPagedPatchStations(StationPage.PageNumber, StationPage.PageSize, FindStationId, PatchId);
  308. });
  309. //更新分页信息
  310. //TotalRecords = result.Item1;
  311. //StationPage.PageCount = result.Item2;
  312. List<VPatchStation> stationList = result.Item3;
  313. foreach(var stationItem in StationItemList)
  314. {
  315. int index = stationItem.Index;
  316. var findItem = stationList.Find((a) => a.StationId == stationItem.StationId);
  317. if(findItem!= null)
  318. {
  319. ObjectHelper.CopyMatchingFields(findItem, stationItem);
  320. stationItem.Index = index;
  321. }
  322. }
  323. }
  324. catch (Exception ex)
  325. {
  326. MessageBox.Show(Application.Current.MainWindow,
  327. $"a加载数据是发生错误:{ex.Message}", "错误",
  328. MessageBoxButton.OK, MessageBoxImage.Error);
  329. }
  330. }
  331. private ScrollViewer _dgStaionScrollViewer;
  332. private ScrollViewer GetDgStationScrollViewer(DependencyObject obj)
  333. {
  334. if (_dgStaionScrollViewer != null)
  335. {
  336. return _dgStaionScrollViewer;
  337. }
  338. if (obj is ScrollViewer)
  339. {
  340. _dgStaionScrollViewer = (ScrollViewer)obj;
  341. return obj as ScrollViewer;
  342. }
  343. for (int i = 0; i < VisualTreeHelper.GetChildrenCount(obj); i++)
  344. {
  345. var child = VisualTreeHelper.GetChild(obj, i);
  346. var scrollViewer = GetDgStationScrollViewer(child);
  347. if (scrollViewer != null)
  348. {
  349. _dgStaionScrollViewer = scrollViewer;
  350. return scrollViewer;
  351. }
  352. }
  353. return null;
  354. }
  355. //---------------------------------------------------------------------
  356. }
  357. }