using System; using System.Linq; using System.Threading.Tasks; using Autodesk.Revit.DB; using Autodesk.Revit.UI; namespace Szmedi.RevitToolkit.Approval.Controls.ProgressWrapper { class ProgressBarManager { private readonly UIDocument uidoc; private readonly IEnumerable collection; //private readonly object addinParameter; private readonly Action action; private readonly string taskName; ProgressMonitorViewModel viewModel; ProgressMonitorView progressBarView; #region 非模态处理 private readonly ProcessEventHandler progressEventHandler; /// /// 非模态进度条,只能开启一次,请勿在遍历中使用 /// /// /// public ProgressBarManager(ProcessEventHandler progressEventHandler, IEnumerable collection, string taskName = "处理任务", object addinParameter = null) { this.progressEventHandler = progressEventHandler; progressEventHandler.Collection = collection; progressEventHandler.AddinParameter = addinParameter; this.collection = collection; this.taskName = taskName; } /// /// 开始处理 /// public void ProgressModeless() { if (!progressEventHandler.Collection.Any()) { return; } viewModel = new() { MaxValue = collection.Count(), Title = taskName }; progressBarView ??= new(); //progressBarView = ProgressMonitorView.Instance; progressBarView.DataContext = viewModel; progressBarView.Loaded += CurrentUI_ContentRendered; progressBarView.Closed += CurrentUI_Closed; progressBarView.Topmost = true; //progressEventHandler = new(collection, action) //{ // ProgressBarView = progressBarView, // ViewModel = viewModel //}; if (collection.Any()) { progressEventHandler.ProgressBarView = progressBarView; progressEventHandler.ViewModel = viewModel; //progressBarView.Show(); progressBarView.Show(); //progressEventHandler.Raise(); } } private void CurrentUI_ContentRendered(object sender, EventArgs e) { progressEventHandler.Raise(); } #endregion #region 模态处理 bool Cancel { get; set; } //private delegate void ProgressBarDelegate(); /// /// 模态进度条 /// /// /// /// public ProgressBarManager(UIDocument uidoc, IEnumerable collection, Action action, string taskName = "处理任务") { this.uidoc = uidoc; this.collection = collection; this.action = action; this.taskName = taskName; } /// /// 开始处理 /// public void ProgressModal() { if (!collection.Any()) { return; } viewModel = new ProgressMonitorViewModel { MaxValue = collection.Count(), Title = taskName }; progressBarView = new ProgressMonitorView { DataContext = viewModel }; //progressBarView = ProgressMonitorView.Instance; //progressBarView.DataContext = viewModel; progressBarView.Closed += CurrentUI_Closed; progressBarView.ContentRendered += FireUPModal; progressBarView.ShowDialog(); } private async void FireUPModal(object sender, EventArgs e) { progressBarView.ContentRendered -= FireUPModal; for (viewModel.CurrentValue = 0; viewModel.CurrentValue < viewModel.MaxValue; viewModel.CurrentValue++) { if (Cancel) break; //System.Threading.Thread.Sleep(50); try { var t = collection.ElementAt(viewModel.CurrentValue); //Debug.WriteLine(ViewModel.CurrentValue); action(uidoc, t); } catch (Exception) { CloseWindow(); } viewModel.CurrentContext = $"处理 {viewModel.CurrentValue} / {viewModel.MaxValue} 完成"; await Task.Delay(10); //progressBarView.Dispatcher.Invoke(viewModel.NotifyUI, System.Windows.Threading.DispatcherPriority.Background); } CloseWindow(); } private void CloseWindow() { progressBarView.Closed -= CurrentUI_Closed; progressBarView.Close(); } private void CurrentUI_Closed(object sender, EventArgs e) { Cancel = true; } #endregion } }