Skip to main content
 首页 » 编程设计

c#之如何在 C# 中使用进度条

2024年02月05日9webabcd

我想使用 ProgressBar 并在 2 秒内将其从 100% 运行到 0%。我已经编写了以下函数,但它似乎无法正常运行。请帮忙!谢谢!

private async void progessbar() 
{ 
    for (iValue = 100; iValue >= 0; iValue--) 
    { 
        pbTime.Value = iValue; 
        await Task.Delay(20); 
    } 
} 

请您参考如下方法:

如果你想流畅地动画你的进度条,你应该使用 Storyboard:

private void AnimateProgressBar() 
{ 
    var storyboard = new Storyboard(); 
 
    var animation = new DoubleAnimation { Duration = TimeSpan.FromSeconds(2), From = 100, To = 0, EnableDependentAnimation = true }; 
 
    Storyboard.SetTarget(animation, this.ProgressBar); 
    Storyboard.SetTargetProperty(animation, "Value"); 
 
    storyboard.Children.Add(animation); 
 
    storyboard.Begin(); 
}