PromptViewModel.cs 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. using HandyControl.Interactivity.Commands;
  2. using Shaker.ViewModel;
  3. using System;
  4. using System.Collections.Generic;
  5. using System.Diagnostics.CodeAnalysis;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Threading.Tasks;
  9. using System.Timers;
  10. using System.Windows;
  11. using System.Windows.Input;
  12. using System.Windows.Media;
  13. namespace ShakerControl.ViewModel
  14. {
  15. internal class PromptViewModel:ViewModelBase<Shaker.Model.ModelBase>
  16. {
  17. static PromptViewModel()
  18. {
  19. }
  20. private uint maxcount = 0;
  21. private uint consumetime = 0;
  22. private PromptViewModel()
  23. {
  24. timer.Stop();
  25. timer.Interval = 1000;
  26. timer.AutoReset = true;
  27. timer.Elapsed += (sender, e) =>
  28. {
  29. consumetime++;
  30. Tools.DispatherInovke.Inovke(() =>
  31. {
  32. if (YesTextFunc != null)
  33. {
  34. YesText = YesTextFunc(consumetime);
  35. }
  36. if (NoTextFunc != null)
  37. {
  38. NoText = NoTextFunc(consumetime);
  39. }
  40. if (consumetime >= maxcount)
  41. {
  42. IsOpen = false;
  43. timer.Stop();
  44. }
  45. });
  46. };
  47. }
  48. public override void Init()
  49. {
  50. base.Init();
  51. maxcount = 0;
  52. consumetime = 0;
  53. timer.Stop();
  54. YesTextFunc = null;
  55. NoTextFunc = null;
  56. YesVisibility = Visibility.Visible;
  57. NoVisibility = Visibility.Visible;
  58. YesText = "是";
  59. NoText = "否";
  60. Title = "提示";
  61. Message = "请确认";
  62. YesAction = null;
  63. NoAction = null;
  64. IsOpen = false;
  65. IconType = IconType.Info;
  66. }
  67. public static PromptViewModel Default { get; } = new PromptViewModel();
  68. public Visibility IconVisibility { get => iconVisibility; set =>UpdateProperty(ref iconVisibility, value); }
  69. public IconType IconType
  70. {
  71. get => iconType;
  72. set
  73. {
  74. if (value != iconType)
  75. {
  76. iconType = value;
  77. }
  78. switch (iconType)
  79. {
  80. case IconType.Info:
  81. default:
  82. IconGeometry = (Geometry)App.Current.FindResource("InfoGeometry");
  83. IconBrush = new SolidColorBrush(Colors.Black);
  84. break;
  85. case IconType.Ask:
  86. IconGeometry = (Geometry)App.Current.FindResource("AskGeometry");
  87. IconBrush = new SolidColorBrush(Colors.DarkOrange);
  88. break;
  89. case IconType.Error:
  90. IconGeometry = (Geometry)App.Current.FindResource("ErrorGeometry");
  91. IconBrush = new SolidColorBrush(Colors.Red);
  92. break;
  93. }
  94. }
  95. }
  96. private SolidColorBrush iconBrush = new SolidColorBrush(Colors.Black);
  97. public SolidColorBrush IconBrush { get => iconBrush; set =>UpdateProperty(ref iconBrush, value); }
  98. public Geometry IconGeometry { get => iconGeometry; set =>UpdateProperty(ref iconGeometry, value); }
  99. [AllowNull]
  100. public Action YesAction { get; set; }
  101. [AllowNull]
  102. public Action NoAction { get; set; }
  103. private bool isOpen = false;
  104. private string title = "提示";
  105. private string message = "请确认";
  106. private string yesText = "是";
  107. private string noText = "否";
  108. private Visibility yesVisibility = Visibility.Visible;
  109. private Visibility noVisibility = Visibility.Visible;
  110. private Visibility iconVisibility = Visibility.Visible;
  111. private IconType iconType = IconType.Info;
  112. private Geometry iconGeometry = (Geometry)App.Current.FindResource("InfoGeometry");
  113. private System.Timers.Timer timer = new System.Timers.Timer();
  114. [AllowNull]
  115. public Func<uint,string> YesTextFunc { get; set; }
  116. [AllowNull]
  117. public Func<uint, string> NoTextFunc { get; set; }
  118. public void SetTimeout(uint timeout)
  119. {
  120. consumetime = 0;
  121. maxcount = timeout;
  122. timer.AutoReset = true;
  123. timer.Start();
  124. }
  125. public bool IsOpen { get => isOpen; set =>UpdateProperty(ref isOpen, value); }
  126. public string Title { get => title; set =>UpdateProperty(ref title, value); }
  127. public string Message { get => message; set =>UpdateProperty( ref message , value); }
  128. public string YesText { get => yesText; set =>UpdateProperty(ref yesText, value); }
  129. public string NoText { get => noText; set =>UpdateProperty(ref noText,value); }
  130. public ICommand YesCommand => new DelegateCommand(() =>
  131. {
  132. IsOpen = false;
  133. YesAction?.Invoke();
  134. });
  135. public ICommand NoCommand => new DelegateCommand(() =>
  136. {
  137. IsOpen = false;
  138. NoAction?.Invoke();
  139. });
  140. public Visibility YesVisibility { get => yesVisibility; set =>UpdateProperty(ref yesVisibility, value); }
  141. public Visibility NoVisibility { get => noVisibility; set =>UpdateProperty(ref noVisibility,value); }
  142. }
  143. }