博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
潜移默化学会WPF--ProcessBar学习(一)
阅读量:4496 次
发布时间:2019-06-08

本文共 4588 字,大约阅读时间需要 15 分钟。

给你个demo吧

前台页面很简单

后台页面代码

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 ProgressBarDemo{    ///     /// MainWindow.xaml 的交互逻辑    ///     public partial class MainWindow : Window    {        public MainWindow()        {            InitializeComponent();            this.Loaded += new RoutedEventHandler(MainWindow_Loaded);        }        void MainWindow_Loaded(object sender, RoutedEventArgs e)        {            ProgressHelper helper = new ProgressHelper();            helper.End += new EventHandler(helper_End);            helper.Failed += new EventHandler(helper_Failed);            helper.Max += new EventHandler(helper_Max);            helper.Progress += new EventHandler(helper_Progress);            helper.UpdateInfo += new EventHandler(helper_UpdateInfo);            helper.Start();        }        void helper_UpdateInfo(object sender, EventArgs e)        {            this.text.Text = sender as String;        }        void helper_Progress(object sender, EventArgs e)        {            int? val = sender as int?;            this.pb.Value = val.Value;        }        void helper_Max(object sender, EventArgs e)        {            int? val = sender as int?;            this.pb.Maximum = val.Value;            this.pb.Minimum = 0;        }        void helper_Failed(object sender, EventArgs e)        {            MessageBox.Show("出现异常失败!");        }        void helper_End(object sender, EventArgs e)        {            MessageBox.Show("正常结束!");        }    }    public class ProgressHelper    {        private SynchronizationContext mainThreadSynContext;        public event EventHandler Progress = null;        public event EventHandler Max = null;        public event EventHandler UpdateInfo = null;        public event EventHandler End = null;        public event EventHandler Failed = null;        public ProgressHelper()        {            mainThreadSynContext = SynchronizationContext.Current;        }        public void Start()        {            new Thread(new ThreadStart(DoWork)).Start();        }        private void DoWork()        {            try            {                int Max = 20;                int count = 0;                PostMax(Max);                while (count++ < Max)                {                    PostText(String.Format("当前第{0}次更新",count));                    Thread.Sleep(1000);                    PostProgress(count);                }                PostEnd(null);            }            catch (Exception)            {                PostFailed(null);            }                    }        private void PostEnd(Object o)        {            mainThreadSynContext.Post(new SendOrPostCallback(SetEnd), o);        }        private void PostFailed(Object o)        {            mainThreadSynContext.Post(new SendOrPostCallback(SetFailed), o);        }        private void PostText(Object text)        {            mainThreadSynContext.Post(new SendOrPostCallback(UpdateTextInfo), text);        }        private void PostProgress(int i)        {            mainThreadSynContext.Post(new SendOrPostCallback(SetProgress),i);        }        void PostMax(object max)        {            mainThreadSynContext.Post(new SendOrPostCallback(SetMax), max);        }        #region 私有方法,无需关注        void SetFailed(Object e)        {            if (Failed != null)            {                Failed(e, null);            }        }        void SetEnd(Object val)        {            if (End != null)            {                End(null, null);            }        }        void SetProgress(Object progress)        {            if (Progress != null)            {                Progress(progress, null);            }        }        void SetMax(Object max)        {            if (Max != null)            {                Max(max, null);            }        }        void UpdateTextInfo(Object text)        {            if (UpdateInfo != null)            {                UpdateInfo(text, null);            }        }        #endregion    }}

 

我相信你一定会 举一反三的

 

刚刚看到一个很好的样式的 关于 progressbar控件的文章  可能对你有用,如果你想你的progressbar有个性的话 

转载于:https://www.cnblogs.com/AaronYang/archive/2012/04/20/2459056.html

你可能感兴趣的文章
nginx反向代理nginx,RealServer日志打印真实ip
查看>>
Visual Studio蛋疼问题解决(1)
查看>>
98%的人没解出的德国面试逻辑题
查看>>
mysql 复制表结构 / 从结果中导入数据到新表
查看>>
fiddler---使用方法2--抓取其他电脑数据包
查看>>
python基础教程——切片
查看>>
android 获取坐标【转】
查看>>
Windows Text Copyer 1.1绿色版
查看>>
内存重叠strcpy\memcpy
查看>>
球的移动(move)
查看>>
页面禁止双击选中
查看>>
打印流
查看>>
TCP/IP模型的一个简单解释
查看>>
解开最后期限的镣铐(转载)
查看>>
Kth Smallest Element in a BST
查看>>
ubuntu14.04利用aliyun安装docker
查看>>
iphone-命令行编译之--xcodebuild
查看>>
shell笔记
查看>>
python的循环,质数和因子的定义
查看>>
Plan : 破晓
查看>>