From f292222ebf7f7ec89d4c933d2aef7812a292fadf Mon Sep 17 00:00:00 2001 From: vpenades <5433822+vpenades@users.noreply.github.com> Date: Wed, 25 Mar 2020 16:28:44 +0100 Subject: [PATCH] Added IProgress and IProgress to ProgressDialog --- src/Ookii.Dialogs.Wpf/ProgressDialog.cs | 25 ++++++++++++++++++++++++- 1 file changed, 24 insertions(+), 1 deletion(-) diff --git a/src/Ookii.Dialogs.Wpf/ProgressDialog.cs b/src/Ookii.Dialogs.Wpf/ProgressDialog.cs index 3a43e86..4601c2e 100644 --- a/src/Ookii.Dialogs.Wpf/ProgressDialog.cs +++ b/src/Ookii.Dialogs.Wpf/ProgressDialog.cs @@ -24,7 +24,7 @@ namespace Ookii.Dialogs.Wpf /// /// [DefaultEvent("DoWork"), DefaultProperty("Text"), Description("Represents a dialog that can be used to report progress to the user.")] - public partial class ProgressDialog : Component + public partial class ProgressDialog : Component , IProgress, IProgress { private class ProgressChangedData { @@ -42,6 +42,7 @@ private class ProgressChangedData private bool _useCompactPathsForDescription; private SafeModuleHandle _currentAnimationModuleHandle; private bool _cancellationPending; + private int _percentProgress; /// /// Event raised when the dialog is displayed. @@ -518,6 +519,24 @@ public void ShowDialog(Window owner, object argument) RunProgressDialog(owner == null ? NativeMethods.GetActiveWindow() : new WindowInteropHelper(owner).Handle, argument); } + /// + /// Updates the dialog's progress bar. + /// + /// The percentage, from 0 to 100, of the operation that is complete. + void IProgress.Report(int value) + { + ReportProgress(value, null, null, null); + } + + /// + /// Updates the dialog's progress bar. + /// + /// The new value of the progress dialog's primary text message, or to leave the value unchanged. + void IProgress.Report(string value) + { + ReportProgress(_percentProgress, value, null, null); + } + /// /// Updates the dialog's progress bar. /// @@ -568,6 +587,10 @@ public void ReportProgress(int percentProgress, string text, string description, throw new ArgumentOutOfRangeException("percentProgress"); if( _dialog == null ) throw new InvalidOperationException(Properties.Resources.ProgressDialogNotRunningError); + + // we need to cache the latest percentProgress so IProgress.Report(text) can report the percent progress correctly. + _percentProgress = percentProgress; + _backgroundWorker.ReportProgress(percentProgress, new ProgressChangedData() { Text = text, Description = description, UserState = userState }); }