快速备案就找 云聚达,过来人求职网,男人
wpf下使用进度条也是非常方便的,如果直接采用循环然后给processbar赋值,理论上是没有问题的,不过这样会卡主主ui线程,我们看到的效果等全部都结束循环后才出现最后的值。
所以需要采用线程或者后台方式给进度条赋值的方式,以下通过线程来触发事件触发的方式来实现给进度条赋值。这样就可以模拟我们在实际过程中处理数据的一种进度方式。
1 using system; 2 using system.collections.generic; 3 using system.linq; 4 using system.text; 5 using system.threading; 6 using system.threading.tasks; 7 using system.windows; 8 using system.windows.controls; 9 using system.windows.data; 10 using system.windows.documents; 11 using system.windows.input; 12 using system.windows.media; 13 using system.windows.media.imaging; 14 using system.windows.navigation; 15 using system.windows.shapes; 16 17 namespace wpftestprocessbar 18 { 19 /// <summary> 20 /// mainwindow.xaml 的交互逻辑 21 /// </summary> 22 public partial class mainwindow : window 23 { 24 public delegate void progressdelegate(int percent); 25 public mainwindow() 26 { 27 initializecomponent(); 28 progressevent += mainwindow_progressevent; 29 beginimport(); 30 } 31 void mainwindow_progressevent(int percent) 32 { 33 dispatcher.invoke(new action<system.windows.dependencyproperty, object>(pro.setvalue), system.windows.threading.dispatcherpriority.background, new object[] { progressbar.valueproperty, convert.todouble(percent+ 1) }); 34 dispatcher.invoke(new action<system.windows.dependencyproperty, object>(label.setvalue), system.windows.threading.dispatcherpriority.background, new object[] { label.contentproperty, convert.tostring((percent + 1)+"%") }); 35 36 } 37 private event progressdelegate progressevent; 38 private void beginimport() 39 { 40 pro.maximum = 100; 41 pro.value = 0; 42 label.content = "0%"; 43 threadpool.queueuserworkitem(state => 44 { 45 thread.sleep(2000); 46 for (int i = 0; i < 100; i++) 47 { 48 if (progressevent != null) 49 { 50 progressevent(i); 51 } 52 thread.sleep(10); 53 } 54 }); 55 } 56 } 57 }
以上只是一种实现方式,希望给有需要的人提供帮助。
效果如下:
如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复
Blazor server side 自家的一些开源的, 实用型项目的进度之 CEF客户端
.NET IoC模式依赖反转(DIP)、控制反转(Ioc)、依赖注入(DI)
vue+.netcore可支持业务代码扩展的开发框架 VOL.Vue 2.0版本发布
网友评论