WPF进度条

San*_*tha 13 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);
}
Run Code Online (Sandbox Code Playgroud)

加载事件如下

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

最后在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);
}
Run Code Online (Sandbox Code Playgroud)

当我执行我的程序时,进度条显示两三个条的进度,然后它停止增量.后来对进展没有任何影响.我的代码出了什么问题.请帮忙!..

关心Sangeetha

Ale*_*lex 10

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

这个其他的SO问题提供了一些有关它的见解.简而言之,它是一个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"/> 
Run Code Online (Sandbox Code Playgroud)

然后在代码中,你这样做:

progressBar1.IsIndeterminate = true; // start animation
progressBar1.IsIndeterminate = false; // stop animation
Run Code Online (Sandbox Code Playgroud)


Hen*_*man 9

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

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

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

这让我感到惊讶,我根本没想到它会起作用.这意味着您也可能遇到其他问题,例如阻塞主(调度程序)线程.

您只在Loaded事件中设置一次Value:

     progressBar1.Value = DateTime.Now.Second;
Run Code Online (Sandbox Code Playgroud)

progressBar1.ValueTick事件没有变化.所以它认为它停止了移动.