ModalActionViewModel.cs 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text;
  4. using System.Threading.Tasks;
  5. using Vinno.IUS.Common.Log;
  6. using Vinno.IUS.Common.Utilities;
  7. namespace vCloud.GeneratePackages.Tool.ViewModels
  8. {
  9. public class ModalActionViewModel : ViewModel
  10. {
  11. IDescriptionUpdater _descriptionUpdater;
  12. private bool _isBusy;
  13. private string _title;
  14. private readonly Action _action;
  15. /// <summary>
  16. /// The event page closed
  17. /// </summary>
  18. public event EventHandler ClosedRequest;
  19. /// <summary>
  20. /// view model is busy
  21. /// </summary>
  22. public bool IsBusy
  23. {
  24. get { return _isBusy; }
  25. set
  26. {
  27. if (_isBusy != value)
  28. {
  29. _isBusy = value;
  30. OnPropertyChanged(() => IsBusy);
  31. }
  32. }
  33. }
  34. /// <summary>
  35. /// Title of this window
  36. /// </summary>
  37. public string Title
  38. {
  39. get { return _title; }
  40. set
  41. {
  42. if (_title != value)
  43. {
  44. _title = value;
  45. OnPropertyChanged(() => Title);
  46. }
  47. }
  48. }
  49. public ModalActionViewModel(Action action, IDescriptionUpdater descriptionUpdater, string descpription = null, string titile = null)
  50. {
  51. Title = titile;
  52. _descriptionUpdater = descriptionUpdater;
  53. if (_descriptionUpdater != null)
  54. _descriptionUpdater.DescriptionUpdated += OnDescriptionUpdated;
  55. _action = action;
  56. if (!string.IsNullOrEmpty(descpription))
  57. {
  58. Description = TranslateHelper.Translate(descpription);
  59. }
  60. OnLoadedAsync();
  61. }
  62. /// <summary>
  63. /// 更新描述信息
  64. /// </summary>
  65. /// <param name="sender"></param>
  66. /// <param name="e"></param>
  67. private void OnDescriptionUpdated(object sender, string e)
  68. {
  69. AppManager.MainDispatcher.Invoke(() => { Description = e; });
  70. }
  71. /// <summary>
  72. /// 加载ftp编译包信息
  73. /// </summary>
  74. private async void OnLoadedAsync()
  75. {
  76. try
  77. {
  78. await DoActionAsync();
  79. }
  80. catch(Exception e)
  81. {
  82. Logger.WriteLineError($"Action {Title} ex:{e}");
  83. OnClosedRequest();
  84. }
  85. }
  86. /// <summary>
  87. /// 阻碍UI直至信息加载完成
  88. /// </summary>
  89. /// <returns></returns>
  90. async Task DoActionAsync()
  91. {
  92. IsBusy = true;
  93. await Task.Run(() =>
  94. {
  95. _action.Invoke();
  96. });
  97. IsBusy = false;
  98. OnClosedRequest();
  99. }
  100. /// <summary>
  101. /// 关闭时反注册事件
  102. /// </summary>
  103. protected virtual void OnClosedRequest()
  104. {
  105. if (_descriptionUpdater != null)
  106. _descriptionUpdater.DescriptionUpdated -= OnDescriptionUpdated;
  107. ClosedRequest?.Invoke(this, EventArgs.Empty);
  108. }
  109. }
  110. }