标签:temp dev img art horizon res main windows 闪烁
原文:TaskBarProgress(任务栏进度条)1 <Window.TaskbarItemInfo>
2 <TaskbarItemInfo />
3 </Window.TaskbarItemInfo>
4
5 <Grid>
6 <ProgressBar x:Name="ProgressBar" Margin="10"
7 HorizontalAlignment="Stretch"
8 VerticalAlignment="Center"
9 Height="23" />
10 </Grid>
1 public partial class MainWindow : Window
2 {
3 public MainWindow()
4 {
5 InitializeComponent();
6
7 Loaded += new RoutedEventHandler(MainWindow_Loaded);
8 }
9
10 void MainWindow_Loaded(object sender, RoutedEventArgs e)
11 {
12 //BackgroundWorker,在单独的线程上执行操作
13 BackgroundWorker worker = new BackgroundWorker();
14
15 //BW.DoWork,调用BW.RunWorkerAsync()时发生
16
17 //初始化命名委托;Lambda 运算符的左边是输入参数(如果有),右边包含表达式或语句块;
18 worker.DoWork += (s, ev) =>
19 {
20 for (int i = 0; i < 100; i += 10)
21 {
22 Thread.Sleep(1000);
23
24 //BW.ReportProgress,引发BW.ProgressChanged事件
25 ((BackgroundWorker)s).ReportProgress(i);
26 }
27 };
28 //调用BW.ReportProgress时发生
29 worker.ProgressChanged += new ProgressChangedEventHandler(worker_ProgressChanged);
30
31 //当后台操作已完成,被取消或引发异常时发生
32 worker.RunWorkerCompleted += new RunWorkerCompletedEventHandler(worker_RunWorkerCompleted);
33
34 //BackgroundWorker.WorkerReportsProgress 获取或设置一个值,该值指示 BackgroundWorker 能否报告进度更新。
35 worker.WorkerReportsProgress = true;
36
37 //TaskbarItemInfo获取或设置 System.Windows.Window 的任务栏缩略图。
38 //TaskbarItemInfo.ProgressState 获取或设置一个值,该值指示在任务栏按钮中显示进度指示器的方式。
39
40 TaskbarItemInfo.ProgressState = TaskbarItemProgressState.Indeterminate;
41
42 //TaskbarItemProgressState枚举值
43 //None=0,Indeterminate=1(闪烁绿色),Normal=2(绿色),Error=3(红色),Paused=4(黄色)
44
45 //开始执行后台操作
46 worker.RunWorkerAsync();
47
48 }
49
50
51 void worker_ProgressChanged(object sender, ProgressChangedEventArgs e)
52 {
53 //ProgressChangedEventArgs.ProgressPercentage 获取异步任务的进度百分比
54 ProgressBar.Value = e.ProgressPercentage;
55
56 // double TaskbarItemInfo.ProgressValue获取或设置一个值,该值指示任务栏按钮中进度指示器的填满状态。
57 TaskbarItemInfo.ProgressValue = (double)e.ProgressPercentage / 100;
58 }
59
60 void worker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
61 {
62 ProgressBar.Value = 100;
63 TaskbarItemInfo.ProgressValue = 1.0;
64 }
65 }
标签:temp dev img art horizon res main windows 闪烁
原文地址:https://www.cnblogs.com/lonelyxmas/p/8986708.html