WPF 进度条在几个条后停止 [英] WPF Progressbar Stops after a Few Bars
问题描述
在我的 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();
}
最后在tick事件中,
And at last in tick event,
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.
为什么?
推荐答案
在我的 WPF 应用程序中,我有...
System.Windows.Forms.Timer timer;
In my WPF application I have ...
System.Windows.Forms.Timer timer;
那是错误的计时器类型.改用 DispatcherTimer.
That is the wrong type of timer. Use a DispatcherTimer instead.
当我执行我的程序时,progressbar 会显示 2-3 bar 的进度,然后它就停止了
When i execute my program progressbar shows the progress for two-three bars and then it stops
这让我很惊讶,我根本没想到它会起作用.这意味着您可能还有其他问题,例如阻塞主(调度程序)线程.罢工>
This surprises me, I wouldn't have expected it to work at all. This means you may have other problems too, like blocking the main (dispatcher) thread.
您只需在 Loaded 事件中设置一次值:
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 进度条在几个条后停止的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!