今天研究了一下wpf的进度条ProgressBar
1.传统ProgressBar
WPF进度条ProgressBar 这个控件,如果直接写到循环里,会死掉,界面会卡死,不会有进度。需要把进度条放到单独的线程中。
传统的需要建立 Thread 或者使用 Timer,分别写在不同的方法中。但现在,使用 Dispatcher.Invoke 调用可以实现这个目的。
for (int i = 0; i <= 10000; i++)
{
double value = i * 100.0 / 10000;
lbShow.Content = "总大小:" + 10000 + ",已导入:" + i;
pbBar.Dispatcher.Invoke(new Action<System.Windows.DependencyProperty, object>(pbBar.SetValue), System.Windows.Threading.DispatcherPriority.Background, ProgressBar.ValueProperty, value);
}
pbBar是ProgressBar的名称。下图是传统进度条显示的效果。
传统的进度条看着比价简单,没有高大上的感觉。所以一般很少受到广大人民的喜好。
2.圆形进度条(这是借鉴了网上一位网友的,如侵犯到版权,请给我留言)
圆形进度条,如下图,同样是用ProgressBar控件,但感觉就会不一样
要显示这个效果,首先要编写一个类,这里编写的类是ValueToProcessConverter.cs类,源码是
using System; using System.Collections.Generic; using System.Globalization; using System.Linq; using System.Text; using System.Windows; using System.Windows.Data; using System.Windows.Media; namespace WpfSqlHelp { class ValueToProcessConverter : IValueConverter { private const double Thickness = 8; private const double Padding = 1; private const double WarnValue = 60; private const int SuccessRateFontSize = 34; private static readonly SolidColorBrush NormalBrush; private static readonly SolidColorBrush WarnBrush; private static readonly Typeface SuccessRateTypeface; private string percentString; private Point centerPoint; private double radius; static ValueToProcessConverter() { NormalBrush = new SolidColorBrush(Colors.Green); WarnBrush = new SolidColorBrush(Colors.Red); SuccessRateTypeface = new Typeface(new FontFamily("MSYH"), n