本文实例为大家分享了wpf progressbar实现实时进度的具体代码,供大家参考,具体内容如下
简单测试,页面如图:
利用上班的一点点空闲时间,做了个wpf progressbar的例子,觉得有潜力可挖,故放上来供大家参考,有写的更好的请留言哦,方便交流学习,谢谢
这个xaml:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
< page x:class = "wpfbrowserappdrill.progressbarpage" xmlns = "http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x = "http://schemas.microsoft.com/winfx/2006/xaml" xmlns:mc = "http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:d = "http://schemas.microsoft.com/expression/blend/2008" mc:ignorable = "d" title = "progressbarpage" loaded = "page_loaded" > < grid > < button margin = "10" click = "button_click" verticalalignment = "top" height = "23" width = "100" >进度条控制</ button > < textblock name = "txtjd" verticalalignment = "top" horizontalalignment = "center" margin = "50" >当前进度:</ textblock > < progressbar height = "20" horizontalalignment = "center" margin = "8,167,0,0" name = "pb_import" verticalalignment = "top" width = "500" /> </ grid > </ page > |
后台代码:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
|
using system; using system.collections.generic; using system.linq; using system.text; using system.windows; using system.windows.controls; using system.windows.data; using system.windows.documents; using system.windows.input; using system.windows.media; using system.windows.media.imaging; using system.windows.navigation; using system.windows.shapes; using system.threading; namespace wpfbrowserappdrill { /// <summary> /// progressbarpage.xaml 的交互逻辑 /// </summary> public partial class progressbarpage : page { public progressbarpage() { initializecomponent(); } private void beginimport() { double value = 0; double total = 100d; //得到循环次数 while (value < total) { double jd = math.round(((value + 1) * (pb_import.maximum / total)), 4); pb_import.dispatcher.invoke( new action<system.windows.dependencyproperty, object >(pb_import.setvalue), system.windows.threading.dispatcherpriority.background, progressbar.valueproperty, jd); //这里是加数据或费时的操作,我这里让它挂起300毫秒 thread.sleep(300); txtjd.text = "当前的进度是:" + (value + 1) + "(实际值)" + jd + "(百分比)" ; value++; } } private void button_click( object sender, routedeventargs e) { beginimport(); ////new thread(new threadstart(beginimport)).start(); } private void page_loaded( object sender, routedeventargs e) { pb_import.maximum = 100; pb_import.value = 0; } } } |
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持服务器之家。
原文链接:https://blog.csdn.net/qiulei_21/article/details/9130271