using Autodesk.Revit.DB; namespace ShrlAlgoToolkit.RevitAddins.Common.Controls { class ProgressBarManager { private readonly UIDocument uidoc; private readonly IEnumerable collection; private readonly object addinParameter; private readonly Action action; private readonly string taskName; private readonly bool useTransaction; 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, ICollection collection, Action action, string taskName = "处理任务", bool useTransaction = true, object addinParameter = null) { this.uidoc = uidoc; this.collection = collection; this.addinParameter = addinParameter; this.action = action; this.taskName = taskName; this.useTransaction = useTransaction; } 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; Transaction ts = null; // 根据条件决定是否创建并启动事务 if (useTransaction) { ts = new Transaction(uidoc.Document, taskName); ts.Start(); } 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, addinParameter); } catch (Exception) { CloseWindow(); // 如果发生异常且事务已启动,则回滚事务 if (useTransaction && ts != null && ts.GetStatus() == TransactionStatus.Started) { ts.RollBack(); } } viewModel.CurrentContext = $"处理 {viewModel.CurrentValue} / {viewModel.MaxValue} 完成"; await Task.Delay(50); //progressBarView.Dispatcher.Invoke(viewModel.NotifyUI, System.Windows.Threading.DispatcherPriority.Background); } // 根据条件决定是否提交事务 if (useTransaction && ts != null) { ts.Commit(); } CloseWindow(); // 确保事务对象被正确处理 ts?.Dispose(); } private void CloseWindow() { progressBarView.Closed -= CurrentUI_Closed; progressBarView.Close(); } private void CurrentUI_Closed(object sender, EventArgs e) { Cancel = true; } #endregion } }