123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120 |
- 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;
- /// <summary>
- /// The event page closed
- /// </summary>
- public event EventHandler ClosedRequest;
- /// <summary>
- /// view model is busy
- /// </summary>
- public bool IsBusy
- {
- get { return _isBusy; }
- set
- {
- if (_isBusy != value)
- {
- _isBusy = value;
- OnPropertyChanged(() => IsBusy);
- }
- }
- }
- /// <summary>
- /// Title of this window
- /// </summary>
- 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();
- }
- /// <summary>
- /// 更新描述信息
- /// </summary>
- /// <param name="sender"></param>
- /// <param name="e"></param>
- private void OnDescriptionUpdated(object sender, string e)
- {
- AppManager.MainDispatcher.Invoke(() => { Description = e; });
- }
- /// <summary>
- /// 加载ftp编译包信息
- /// </summary>
- private async void OnLoadedAsync()
- {
- try
- {
- await DoActionAsync();
- }
- catch(Exception e)
- {
- Logger.WriteLineError($"Action {Title} ex:{e}");
- OnClosedRequest();
- }
- }
- /// <summary>
- /// 阻碍UI直至信息加载完成
- /// </summary>
- /// <returns></returns>
- async Task DoActionAsync()
- {
- IsBusy = true;
- await Task.Run(() =>
- {
- _action.Invoke();
- });
- IsBusy = false;
- OnClosedRequest();
- }
- /// <summary>
- /// 关闭时反注册事件
- /// </summary>
- protected virtual void OnClosedRequest()
- {
- if (_descriptionUpdater != null)
- _descriptionUpdater.DescriptionUpdated -= OnDescriptionUpdated;
- ClosedRequest?.Invoke(this, EventArgs.Empty);
- }
- }
- }
|