c# - WPF 进度条在几条之后停止

标签 c# wpf timer progress-bar

在我的 WPF 应用程序中,我必须在计时器滴答事件中显示进度条进度,我如下编写,

System.Windows.Forms.Timer timer;
public MainWindow()
{
    timer = new System.Windows.Forms.Timer();
    timer.Interval = 1000;
    this.timer.Tick += new System.EventHandler(this.timer_Tick);
}

加载事件如下

private void Window_Loaded(object sender, RoutedEventArgs e)
{
      progressBar1.Minimum = 0;
      progressBar1.Value = DateTime.Now.Second;
      progressBar1.Maximum = 700;
      timer.Start();         
 }

最后在 tick 事件中,

private void timer_Tick(object sender, EventArgs e)
{
    Duration duration = new Duration(TimeSpan.FromSeconds(20));

    //progress bar animation
    System.Windows.Media.Animation.DoubleAnimation doubleanimation = new    System.Windows.Media.Animation.DoubleAnimation(200.0, duration);
    progressBar1.BeginAnimation(ProgressBar.ValueProperty, doubleanimation);
}

当程序的进度条显示两到三个条的进度然后停止递增。后来对进度完全没有影响。

为什么?

最佳答案

由于您的 ProgressBar 与任何特定行为无关,因此它看起来像是一个不确定 栏的工作。

This other SO question提供了一些关于它的见解。简而言之,它是一个 XAML 单行代码:

<!-- MinVal, MaxVal, Height needed for this to work -->
<ProgressBar x:Name="progressBar1" Margin="5" IsIndeterminate="True" 
    MinimumValue="0" MaximumValue="700" value="0" Height="20"/> 

然后在代码中,你是这样的:

progressBar1.IsIndeterminate = true; // start animation
progressBar1.IsIndeterminate = false; // stop animation

关于c# - WPF 进度条在几条之后停止,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15429256/

相关文章:

java - 如何使计时器倒计时以及进度条?

c# - MySQL Linq 使用 .Contains(变量)

c# - 如何清除列表框中的所有数据?

c# - WPF 数据绑定(bind)需要手动提升

wpf - 在调整其列大小时更改 DataGrid 宽度

xamarin - 如何在完成之前取消计时器

c# - AudioPlayerAgent、计时器和网络服务

C#计算股票和指数之间的投资组合贝塔值

c# - 从 C# 迁移到 C++(加上在同一解决方案中使用两者)

c# - ObservableCollection<T> 由其他 7 个 ObservableCollection<T> 集合组成