前端代码
<ProgressBar Name="pbar" Margin="350,6,120,6" VerticalAlignment="Center" Height="36" Minimum="0" Maximum="1" >
<ProgressBar.Foreground>
<LinearGradientBrush >
<GradientStop Color="#1296DB" Offset="0"/>
</LinearGradientBrush>
</ProgressBar.Foreground>
</ProgressBar>
<TextBlock Name="txtPercent" Margin="620,12,270,10" RenderTransformOrigin="0.812,0.5" VerticalAlignment="Center"/>
后台代码:
for(int i=1;i<=1000;i++)\
{
double l = (double)i/ 1000;
pbar.Dispatcher.Invoke(new Action<System.Windows.DependencyProperty, object>(pbar.SetValue), System.Windows.Threading.DispatcherPriority.Background, ProgressBar.ValueProperty, l);//更新进度条的进度;
this.txtPercent.Dispatcher.Invoke(new Action(() => { txtPercent.Text = Convert.ToInt32((l * 100)).ToString() + "%"; })); //使用一个textBlock控件,在进度条上显示百分比;
}
// Dispatcher.Invoke(new Action<System.Windows.DependencyProperty, object>(Pro.SetValue), System.Windows.Threading.DispatcherPriority.Background, new object[] { ProgressBar.ValueProperty, Convert.ToDouble(percent+ 1) });