59 lines
1.6 KiB
C#
59 lines
1.6 KiB
C#
|
|
using System;
|
||
|
|
using System.Threading.Tasks;
|
||
|
|
using System.Windows;
|
||
|
|
|
||
|
|
namespace BetterEN5.UI
|
||
|
|
{
|
||
|
|
public partial class ProgressDialog : Window
|
||
|
|
{
|
||
|
|
public ProgressDialog()
|
||
|
|
{
|
||
|
|
InitializeComponent();
|
||
|
|
Loaded += (s, e) =>
|
||
|
|
{
|
||
|
|
var desktop = System.Windows.SystemParameters.WorkArea;
|
||
|
|
Left = (desktop.Width - Width) / 2;
|
||
|
|
Top = (desktop.Height - Height) / 2;
|
||
|
|
};
|
||
|
|
}
|
||
|
|
|
||
|
|
public void UpdateStatus(string text, string detail = "")
|
||
|
|
{
|
||
|
|
Dispatcher.Invoke(() =>
|
||
|
|
{
|
||
|
|
StatusText.Text = text;
|
||
|
|
if (!string.IsNullOrEmpty(detail))
|
||
|
|
DetailText.Text = detail;
|
||
|
|
});
|
||
|
|
}
|
||
|
|
|
||
|
|
public async Task RunWithProgress(Func<ProgressDialog, Task> action)
|
||
|
|
{
|
||
|
|
try
|
||
|
|
{
|
||
|
|
await action(this);
|
||
|
|
UpdateStatus("完成", "");
|
||
|
|
await Task.Delay(500);
|
||
|
|
DialogResult = true;
|
||
|
|
}
|
||
|
|
catch (Exception ex)
|
||
|
|
{
|
||
|
|
UpdateStatus($"失败: {ex.Message}", "");
|
||
|
|
await Task.Delay(1500);
|
||
|
|
DialogResult = false;
|
||
|
|
}
|
||
|
|
Close();
|
||
|
|
}
|
||
|
|
|
||
|
|
public static async Task<bool> Show(Window owner, string title, Func<ProgressDialog, Task> action)
|
||
|
|
{
|
||
|
|
var dialog = new ProgressDialog();
|
||
|
|
dialog.Owner = owner;
|
||
|
|
dialog.Title = title;
|
||
|
|
dialog.Show();
|
||
|
|
await dialog.RunWithProgress(action);
|
||
|
|
return dialog.DialogResult ?? false;
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|