【问题标题】:Animate ProgressBar content WPF with specific value具有特定值的动画 ProgressBar 内容 WPF
【发布时间】:2013-07-11 13:35:45
【问题描述】:

我正在尝试开发一个ProgressBar,它会相应地填充我手动设置的值。比如我有这个ProgressBar

<ProgressBar Height="33" HorizontalAlignment="Left" Name="progressBar1" VerticalAlignment="Top" Width="285" />

我有一个按钮,每次按下它时,ProgressBar 的值会增加 10 个单位,如下所示:

private void button1_Click(object sender, RoutedEventArgs e)
{
    progressBar1.Value += 10;
}

我想在每次单击时为该值更改设置动画。我试过这个:

Duration duration = new Duration(TimeSpan.FromSeconds(1));
DoubleAnimation doubleanimation = new DoubleAnimation(200.0, duration);
progressBar1.BeginAnimation(ProgressBar.ValueProperty, doubleanimation);

但是ProgressBar 的值是从 0 到 100。如何让动画在特定值上停止,而不是 100%?

【问题讨论】:

    标签: c# .net wpf progress-bar


    【解决方案1】:

    如果您执行以下操作,您实际上是在将 Value 设置为最终值 200:

    DoubleAnimation doubleanimation = new DoubleAnimation(200.0, duration);
    

    改为将第一个参数更改为您想要设置动画的值。你的事件处理程序应该是这样的:

    private void button1_Click(object sender, RoutedEventArgs e)
    {
        Duration duration = new Duration(TimeSpan.FromSeconds(1));
        DoubleAnimation doubleanimation = new DoubleAnimation(progressBar1.Value + 10, duration);
        progressBar1.BeginAnimation(ProgressBar.ValueProperty, doubleanimation);
    }
    

    【讨论】:

      【解决方案2】:

      DoubleAnimation Constructor (Double, Duration)第一个参数是

      动画的目标值。

      所以改变这个

      DoubleAnimation doubleanimation = new DoubleAnimation(200.0, duration);
      

      DoubleAnimation doubleanimation = new DoubleAnimation(progressBar1.Value, duration);
      

      【讨论】:

      • But it goes from 0 to 100 value of the ProgressBar. How can I tell the animation to stop on a specific value, instead of going to 100%?我的回答就是基于这个问题
      • 其实,如果你没有注意到,他们会说同样的话。
      • (3 年后)@Cullub 他们没有,Bolu 的答案是将 value 属性从其原始值动画到......它的原始值,它不会做任何事情
      • @Treycos 哇,你是对的。我认为它们的意思是一样的,但 Bolu 的答案应该是 DoubleAnimation(progressBar1.Value + 10, ... 而不仅仅是当前值。
      【解决方案3】:

      对于那些感兴趣的人,我在this 链接中找到了解决方案。它解释了如何使用BackgroundWorker 填充ProgressBar 值。

      我写了这段代码:

       public MainWindow()
       {
          InitializeComponent();
      
          backgroundworker.WorkerReportsProgress = true;
          backgroundworker.WorkerSupportsCancellation = true;
          backgroundworker.DoWork += backgroundworker_DoWork;
          backgroundworker.ProgressChanged += backgroundworker_ProgressChanged;
      }
      
      private void buttonStop_Click(object sender, RoutedEventArgs e)
      {
          backgroundworker.CancelAsync();
          e.Handled = true;
      }
      
      private void buttonStart_Click(object sender, RoutedEventArgs e)
      {
          if (backgroundworker.IsBusy == false)
          {
             backgroundworker.RunWorkerAsync();
          }
          e.Handled = true;
      }
      
      void backgroundworker_ProgressChanged(object sender, ProgressChangedEventArgs e)
      {
          progressBar1.Value = e.ProgressPercentage;
      }
      
      void backgroundworker_DoWork(object sender, DoWorkEventArgs e)
      {
          BackgroundWorker worker = sender as BackgroundWorker;
          if (worker != null)
          {
              for (int i = 0; i <= 10; i++)
              {
                  if (worker.CancellationPending)
                  {
                      e.Cancel = true;
                      break;
                  }
      
                  System.Threading.Thread.Sleep(50);
                  worker.ReportProgress(i);
              }
          }
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-05-26
        • 1970-01-01
        • 2022-07-19
        • 1970-01-01
        • 2021-11-14
        • 1970-01-01
        相关资源
        最近更新 更多