WPF进度栏停止几个酒吧后 [英] WPF Progressbar Stops after a Few Bars

查看:67
本文介绍了WPF进度栏停止几个酒吧后的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的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.

为什么?

推荐答案

在我的 WPF 应用程序中,我有... System.Windows.Forms.Timer计时器;

In my WPF application I have ... System.Windows.Forms.Timer timer;

这是错误的计时器类型.改用 DispatcherTimer .

That is the wrong type of timer. Use a DispatcherTimer instead.

当我执行程序时,进度栏显示两三个进度条的进度,然后停止

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.

在已加载"事件中,您只需设置一次值:

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆