转:幻影星辰
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类,源码是

其次要在相应wpf窗体上做相应修改
(1)在窗体上的xaml代码为
<Window x:Class="WpfSqlHelp.TelNumWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:WpfSqlHelp"
Title="TelNumWindow" Height="300" Width="300">
<Window.Resources>
<local:ValueToProcessConverter x:Key="ValueToProcessConverter"/>
</Window.Resources>
<Grid>
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="30"></RowDefinition>
<RowDefinition Height="30"></RowDefinition>
<RowDefinition></RowDefinition>
</Grid.RowDefinitions>
<Button Name="btnInput" Content="数据导入" HorizontalAlignment="Left" Margin="77,5,0,0" VerticalAlignment="Top" Width="75" Click="btnInput_Click"/>
<Label Name="lbShow" Grid.Row="1" HorizontalAlignment="Left" Margin="38,5,0,0" VerticalAlignment="Top" RenderTransformOrigin="-0.375,0.3"/>
<ProgressBar Name="pbBar" Grid.Row="2" Minimum="0"
Maximum="100"
Value="{Binding SuccessRate, Mode=OneWay}">
<ProgressBar.Template>
<ControlTemplate TargetType="ProgressBar">
<Border Background="{TemplateBinding Value, Converter={StaticResource ValueToProcessConverter}, ConverterParameter=200}"/>
</ControlTemplate>
</ProgressBar.Template>
</ProgressBar>
</Grid>
</Grid>
</Window>
红色标注的是需要注意修改的地方。
(2)在代码中的编写为
namespace WpfSqlHelp
{
/// <summary>
/// TelNumWindow.xaml 的交互逻辑
/// </summary>
public partial class TelNumWindow : Window, INotifyPropertyChanged
{
public TelNumWindow()
{
InitializeComponent();
}
private void btnInput_Click(object sender, RoutedEventArgs e)
{
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);
}
}
#region INotifyPropertyChanged
public event PropertyChangedEventHandler PropertyChanged;
public void OnPropertyChanged(string propertyName)
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}
#endregion
}
}
如果这些都搞好,运行你就会看到圆形进度条。
发表评论:
◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。