BusyBarViewModel.cs 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. using GSG.NET.Utils;
  2. using Prism.Commands;
  3. using Prism.Mvvm;
  4. using Prism.Services.Dialogs;
  5. using System;
  6. using System.Collections.Generic;
  7. using System.Linq;
  8. using System.Text;
  9. using System.Threading.Tasks;
  10. using System.Windows.Controls;
  11. using System.Windows.Threading;
  12. namespace OHV.Module.Interactivity.PopUp
  13. {
  14. public class BusyBarViewModel : BindableBase, IDialogAware
  15. {
  16. private DelegateCommand<string> _closeDialogCommand;
  17. public event Action<IDialogResult> RequestClose;
  18. public DelegateCommand<string> CloseDialogCommand =>
  19. _closeDialogCommand ?? (_closeDialogCommand = new DelegateCommand<string>(CloseDialog));
  20. //public string Title => throw new NotImplementedException();
  21. #region Property
  22. private string text = "Waiting. . .";
  23. public string Text
  24. {
  25. get { return text; }
  26. set { SetProperty(ref this.text, value); }
  27. }
  28. private string title = "Device";
  29. public string Title
  30. {
  31. get { return title; }
  32. set { SetProperty(ref this.title, value); }
  33. }
  34. private bool isBusy;
  35. public bool IsBusy
  36. {
  37. get { return isBusy; }
  38. set { SetProperty(ref this.isBusy , value); }
  39. }
  40. #endregion
  41. public BusyBarViewModel()
  42. {
  43. }
  44. public void Init() { }
  45. #region Dialog Function
  46. public bool CanCloseDialog()
  47. {
  48. return true;
  49. }
  50. private int progressValue;
  51. public int ProgressValue
  52. {
  53. get { return progressValue; }
  54. set { SetProperty(ref this.progressValue , value); }
  55. }
  56. public void OnDialogClosed()
  57. {
  58. }
  59. DispatcherTimer busyTimer;
  60. public void OnDialogOpened(IDialogParameters parameters)
  61. {
  62. //this.ConfigListProperty = sql.ConfigDal.GetAll().ToList();
  63. this.Title = parameters.GetValue<string>("Title");
  64. this.Text = parameters.GetValue<string>("Text");
  65. var sw = SwUtils.CurrentTimeMillis;
  66. busyTimer = new DispatcherTimer();
  67. busyTimer.Tick += (object sender, EventArgs e) =>
  68. {
  69. if (SwUtils.Gt(sw, 10000))
  70. {
  71. this.IsBusy = false;
  72. this.CloseDialog("");
  73. }
  74. if (this.IsBusy)
  75. {
  76. this.IsBusy = false;
  77. this.CloseDialog("");
  78. }
  79. this.ProgressValue += 5;
  80. };
  81. busyTimer.Interval = TimeSpan.FromMilliseconds(200);
  82. busyTimer.Start();
  83. }
  84. private void CloseDialog(string parameter)
  85. {
  86. ButtonResult result = ButtonResult.None;
  87. if (parameter?.ToLower() == "true")
  88. result = ButtonResult.OK;
  89. else if (parameter?.ToLower() == "false")
  90. result = ButtonResult.Cancel;
  91. result = ButtonResult.OK;
  92. RaiseRequestClose(new DialogResult(result));
  93. }
  94. public virtual void RaiseRequestClose(IDialogResult dialogResult)
  95. {
  96. this.busyTimer.Stop();
  97. this.busyTimer = null;
  98. RequestClose?.Invoke(dialogResult);
  99. }
  100. #endregion
  101. }
  102. }