这期内容当中小编将会给大家带来有关WPF中怎么通过线程使用ProcessBar,文章内容丰富且以专业的角度为大家分析和叙述,阅读完这篇文章希望大家可以有所收获。
方法示例:
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading; using System.Threading.Tasks; 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; namespace WpfTestProcessBar { /// <summary> /// MainWindow.xaml 的交互逻辑 /// </summary> public partial class MainWindow : Window { public delegate void ProgressDelegate(int percent); public MainWindow() { InitializeComponent(); ProgressEvent += MainWindow_ProgressEvent; beginImport(); } void MainWindow_ProgressEvent(int percent) { Dispatcher.Invoke(new Action<System.Windows.DependencyProperty, object>(Pro.SetValue), System.Windows.Threading.DispatcherPriority.Background, new object[] { ProgressBar.ValueProperty, Convert.ToDouble(percent+ 1) }); Dispatcher.Invoke(new Action<System.Windows.DependencyProperty, object>(label.SetValue), System.Windows.Threading.DispatcherPriority.Background, new object[] { Label.ContentProperty, Convert.ToString((percent + 1)+"%") }); } private event ProgressDelegate ProgressEvent; private void beginImport() { Pro.Maximum = 100; Pro.Value = 0; label.Content = "0%"; ThreadPool.QueueUserWorkItem(state => { Thread.Sleep(2000); for (int i = 0; i < 100; i++) { if (ProgressEvent != null) { ProgressEvent(i); } Thread.Sleep(10); } }); } } }
上述就是小编为大家分享的WPF中怎么通过线程使用ProcessBar了,如果刚好有类似的疑惑,不妨参照上述分析进行理解。如果想知道更多相关知识,欢迎关注亿速云行业资讯频道。
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。