using System; using System.Collections.Generic; using System.Text; using System.Threading.Tasks; using Vinno.IUS.Common.Log; using Vinno.IUS.Common.Utilities; namespace vCloud.GeneratePackages.Tool.ViewModels { public class ModalActionViewModel : ViewModel { IDescriptionUpdater _descriptionUpdater; private bool _isBusy; private string _title; private readonly Action _action; /// /// The event page closed /// public event EventHandler ClosedRequest; /// /// view model is busy /// public bool IsBusy { get { return _isBusy; } set { if (_isBusy != value) { _isBusy = value; OnPropertyChanged(() => IsBusy); } } } /// /// Title of this window /// public string Title { get { return _title; } set { if (_title != value) { _title = value; OnPropertyChanged(() => Title); } } } public ModalActionViewModel(Action action, IDescriptionUpdater descriptionUpdater, string descpription = null, string titile = null) { Title = titile; _descriptionUpdater = descriptionUpdater; if (_descriptionUpdater != null) _descriptionUpdater.DescriptionUpdated += OnDescriptionUpdated; _action = action; if (!string.IsNullOrEmpty(descpription)) { Description = TranslateHelper.Translate(descpription); } OnLoadedAsync(); } /// /// 更新描述信息 /// /// /// private void OnDescriptionUpdated(object sender, string e) { AppManager.MainDispatcher.Invoke(() => { Description = e; }); } /// /// 加载ftp编译包信息 /// private async void OnLoadedAsync() { try { await DoActionAsync(); } catch(Exception e) { Logger.WriteLineError($"Action {Title} ex:{e}"); OnClosedRequest(); } } /// /// 阻碍UI直至信息加载完成 /// /// async Task DoActionAsync() { IsBusy = true; await Task.Run(() => { _action.Invoke(); }); IsBusy = false; OnClosedRequest(); } /// /// 关闭时反注册事件 /// protected virtual void OnClosedRequest() { if (_descriptionUpdater != null) _descriptionUpdater.DescriptionUpdated -= OnDescriptionUpdated; ClosedRequest?.Invoke(this, EventArgs.Empty); } } }