【发布时间】:2013-03-15 09:55:10
【问题描述】:
在我的 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);
}
当程序的进度条显示 2-3 条的进度时,它会停止递增。以后对进度完全没有影响。
为什么?
【问题讨论】:
-
为什么要使用计时器来启动动画?而且您使用了错误的计时器。
System.Windows.Forms.Timer用于 winforms 应用程序。请改用DispatcherTimer。
标签: c# wpf timer progress-bar