问题描述
在我的WPF应用程序中,我必须在计时器滴答事件中显示进度条进度,该事件如下所示
In my WPF application i have to show a progressbar progress with in a timer tick event, which i am writing as below,
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();
}
最后在滴答事件中,
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);
}
当程序的进度条显示两三个进度条的进度,然后停止增量.后来,进度完全没有效果.
When the program's progressbar shows the progress for two-three bars and then it stops increment. Later there is no effect in the progress at all.
为什么?
推荐答案
这是错误的计时器类型.改用 DispatcherTimer .
That is the wrong type of timer. Use a DispatcherTimer instead.
这让我感到惊讶,我根本没想到它能正常工作.
This surprises me, I wouldn't have expected it to work at all.
在已加载"事件中,您只需设置一次值:
You are only setting the Value once, in the Loaded event:
progressBar1.Value = DateTime.Now.Second;
在Tick事件中, progressBar1.Value
不变.因此,它表明它停止了运动.
There is no change to progressBar1.Value
in the Tick event. So it figures that it stops moving.
这篇关于WPF进度栏停止几个酒吧后的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!