如何更新进度条,因此增加了顺利? [英] How to update a progress bar so it increases smoothly?
问题描述
我使用WPF(C#)的进度条来形容这个过程的进度
I'm using progress bar of WPF (C#) to describe the process's progress.
我的算法如下:
DoSomethingCode1();
ProgressBar.SetPercent(10); // 10%
DoSomethingCode2();
ProgressBar.SetPercent(20); // 20%
...
DoSomethingCode10();
ProgressBar.SetPercent(100); // 100%
这是确定的,但它将使进度条不相继式。
It's ok, but it will make the progress bar was not sequent.
有人能告诉我一些建议,使进度条轻轻地更新?
Someone can tell me some suggestions that make the progress bar is updated softly?
推荐答案
您可以调用 BeginAnimation
方法动画的进度
值
属性。在我下面的例子中,我使用了 DoubleAnimation是
You could call the BeginAnimation
method to animate the ProgressBar
's Value
property. In my example below, I used a DoubleAnimation
.
我创建了一个发生在所需的百分比扩展方法:
I created an extension method that takes in the desired percentage:
public static class ProgressBarExtensions
{
private static TimeSpan duration = TimeSpan.FromSeconds(2);
public static void SetPercent(this ProgressBar progressBar, double percentage)
{
DoubleAnimation animation = new DoubleAnimation(percentage, duration);
progressBar.BeginAnimation(ProgressBar.ValueProperty, animation);
}
}
所以在你的代码,你可以简单地调用:
So in your code you could simply call:
myProgressBar.SetPercent(50);
这样做,根本出平滑过渡,所以它看起来更好。引用另一个回答:我们的想法是,一个进度条报告的实际进展的 - 没有时间已过。它不打算成为一个动画,仅仅表明的事情正在发生的。但是,进度条的默认样式确实有这可能意味着工作发生脉动的效果。
Doing this simply smooths out the transition so it looks nicer. To quote another answer: "The idea is that a progress bar reports actual progress - not time elapsed. It's not intended to be an animation that just indicates something is happening." However, the default style of the progress bar does have a pulsating effect which can imply work is happening.
这篇关于如何更新进度条,因此增加了顺利?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!