WPF 进度对话框控件库
$ dotnet add package XyProgressDialog一个现代化的 WPF 进度对话框组件,支持滚动动画和 Fluent Design 风格。
Install-Package XyProgressDialog
dotnet add package XyProgressDialog
<PackageReference Include="XyProgressDialog" Version="1.0.0" />
using XyProgressDialog.ViewModels;
// 显示进度对话框并执行任务
ProgressExecutor.Execute("处理文件", reporter =>
{
for (int i = 0; i <= 100; i++)
{
// 报告进度(0-1 之间的值)
reporter.Report(i / 100.0);
// 执行实际工作
Thread.Sleep(50);
// 检查是否被取消
if (reporter.IsCancelled)
break;
}
});
滚动动画模式(进度不确定):
┌─────────────────────────────────┐
│ 连接服务器 │
│ 处理中... │
│ ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ │
│ [▮▮▮▮ → → → →] │ ← 循环滚动
└─────────────────────────────────┘
确定进度模式:
┌─────────────────────────────────┐
│ 处理文件 │
│ 处理中... │
│ ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ │
│ [■■■■■■■■░░░░░░░░░░] 45.5%│ ← 精确百分比
└─────────────────────────────────┘
ProgressExecutor.Execute("下载文件", reporter =>
{
var totalBytes = 1000000;
var downloadedBytes = 0;
while (downloadedBytes < totalBytes)
{
// 模拟下载
var chunk = Math.Min(10000, totalBytes - downloadedBytes);
downloadedBytes += chunk;
Thread.Sleep(50);
// 报告进度
reporter.Report((double)downloadedBytes / totalBytes);
if (reporter.IsCancelled)
break;
}
});
ProgressExecutor.Execute("初始化", reporter =>
{
// 第一阶段:连接(显示滚动动画)
reporter.Report(0);
ConnectToServer();
// 第二阶段:加载数据(显示具体进度)
var items = GetItemList();
for (int i = 0; i < items.Count; i++)
{
ProcessItem(items[i]);
reporter.Report((i + 1) / (double)items.Count);
if (reporter.IsCancelled)
break;
}
});
ProgressExecutor.Execute(
title: "批量处理",
action: reporter =>
{
// 你的处理逻辑
for (int i = 0; i <= 100; i++)
{
reporter.Report(i / 100.0);
Thread.Sleep(50);
}
},
canCancel: true, // 是否允许取消(默认 true)
autoClose: true, // 完成后自动关闭(默认 true)
autoCloseDelay: 500 // 自动关闭延迟毫秒数(默认 500)
);
try
{
ProgressExecutor.Execute("处理数据", reporter =>
{
for (int i = 0; i <= 100; i++)
{
if (reporter.IsCancelled)
throw new OperationCanceledException("用户取消操作");
reporter.Report(i / 100.0);
ProcessData(i);
}
});
}
catch (OperationCanceledException)
{
MessageBox.Show("操作已取消");
}
catch (Exception ex)
{
MessageBox.Show($"处理失败:{ex.Message}");
}
public static void Execute(
string title, // 窗口标题
Action<ProgressReporter> action, // 要执行的任务
bool canCancel = true, // 是否允许取消
bool autoClose = true, // 完成后是否自动关闭
int autoCloseDelay = 500 // 自动关闭延迟(毫秒)
)
public class ProgressReporter
{
// 报告进度(0-1 之间的值)
public void Report(double progress);
// 检查是否被取消
public bool IsCancelled { get; }
}
本项目采用 MIT 许可证。
欢迎贡献!请随时提交 Pull Request 或创建 Issue。