我正在使用 WPF (C#) 的进度条来描述进程的进度。
我的算法如下:
DoSomethingCode1();
ProgressBar.SetPercent(10); // 10%
DoSomethingCode2();
ProgressBar.SetPercent(20); // 20%
...
DoSomethingCode10();
ProgressBar.SetPercent(100); // 100%
可以,但是会导致进度条不连续。
谁能告诉我一些让进度条柔和更新的建议?
最佳答案
你可以使用一个行为!
public class ProgressBarSmoother
{
public static double GetSmoothValue(DependencyObject obj)
{
return (double)obj.GetValue(SmoothValueProperty);
}
public static void SetSmoothValue(DependencyObject obj, double value)
{
obj.SetValue(SmoothValueProperty, value);
}
public static readonly DependencyProperty SmoothValueProperty =
DependencyProperty.RegisterAttached("SmoothValue", typeof(double), typeof(ProgressBarSmoother), new PropertyMetadata(0.0, changing));
private static void changing(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
var anim = new DoubleAnimation((double)e.OldValue, (double)e.NewValue, new TimeSpan(0,0,0,0,250));
(d as ProgressBar).BeginAnimation(ProgressBar.ValueProperty, anim, HandoffBehavior.Compose);
}
}
您的 XAML 将如下所示:
<ProgressBar local:ProgressBarSmoother.SmoothValue="{Binding Progress}">
每当您在 xaml 中绑定(bind)到的 Progress
属性发生变化时,ProgressBarSmoother 行为中的代码将运行,为您添加动画到进度条,并为 设置适当的值
和 来自
!
关于c# - 如何更新进度条使其平稳增加?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14485818/