123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596 |
- using System;
- using System.ComponentModel;
- using System.Linq;
- using System.Windows.Media.Imaging;
- namespace InABox.WPF
- {
- public class ProgressSection
- {
- public ProgressSection(string message, Action action)
- {
- Message = message;
- Action = action;
- }
- public string Message { get; set; }
- public Action Action { get; set; }
- }
- public static class Progress
- {
- private static ProgressForm form;
- public static BitmapImage DisplayImage { get; set; }
- public static bool IsVisible => form != null;
- public static string Message => form?.GetMessage();
- public static void ShowModal(params ProgressSection[] sections)
- {
- if (!sections.Any())
- return;
- var progress = new ProgressForm();
- progress.DisplayImage = DisplayImage;
- progress.Activated += (_, args) =>
- {
- progress.Dispatcher.Invoke(() =>
- {
- foreach (var section in sections)
- {
- progress.Progress.Content = section.Message;
- section.Action();
- }
- progress.Close();
- });
- };
- progress.ShowActivated = true;
- progress.ShowDialog();
- }
- public static void ShowModal(string message, Action<IProgress<string>> work)
- {
- var progress = new ProgressForm();
- progress.DisplayImage = DisplayImage;
- progress.Progress.Content = message;
- progress.Loaded += (_, args) =>
- {
- var worker = new BackgroundWorker();
- var update = new Progress<string>(data => progress.Progress.Content = data);
- worker.DoWork += (o, e) => work(update);
- worker.RunWorkerCompleted +=
- (o, e) => progress.Close();
- worker.RunWorkerAsync();
- };
- progress.ShowDialog();
- }
- public static void Show(string message)
- {
- if (form == null)
- {
- form = new ProgressForm();
- form.DisplayImage = DisplayImage;
- form.Show();
- }
- form.UpdateWindow(message);
- }
- public static void SetMessage(string message)
- {
- form.UpdateWindow(message);
- }
- public static void Close()
- {
- form?.CloseWindow();
- form = null;
- }
- }
- }
|