潜移默化学会WPF--ProcessBar学习(一)
给你个demo吧
前台页面很简单
<Window x:Class="ProgressBarDemo.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Title="MainWindow" Height="350" Width="525"> <Grid> <Grid.RowDefinitions> <RowDefinition Height="131*" /> <RowDefinition Height="180*" /> </Grid.RowDefinitions> <TextBlock Name="text" Grid.Row="0"></TextBlock> <ProgressBar Name="pb" Grid.Row="1"></ProgressBar> </Grid> </Window>
后台页面代码
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 { /// <summary> /// MainWindow.xaml 的交互逻辑 /// </summary> 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有个性的话 http://www.cnblogs.com/mgen/archive/2011/10/27/2226635.html