WPF进度条在几条后停止

本文关键字:几条 WPF | 更新日期: 2023-09-27 18:04:16

在我的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);
}

加载事件

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

最后在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);
}

当程序的进度条显示了2 - 3条的进度,然后它停止递增。之后,进度就没有任何影响了。

为什么?

WPF进度条在几条后停止

由于您的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"/> 

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

progressBar1.IsIndeterminate = true; // start animation
progressBar1.IsIndeterminate = false; // stop animation

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

这是错误的定时器类型。使用DispatcherTimer来代替。

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

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

你只设置一次值,在加载事件中:

     progressBar1.Value = DateTime.Now.Second;

Tick事件中progressBar1.Value没有变化。所以它认为它停止了移动

使用DispatcherTimer代替Timer(窗体对象),并使用ProgressBar的Value属性

试试这个:

MainWindows.xaml:

<Window x:Class="WpfApplication1.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="55" Width="261">
    <Grid>
        <ProgressBar Name="pb" Maximum="60" />
    </Grid>
</Window>

MainWindows.xaml.cs:

using System.Windows;
using System.Windows.Threading;
namespace WpfApplication1
{
    public partial class MainWindow : Window
    {
        private DispatcherTimer timer;
    
        public MainWindow()
        {
            InitializeComponent();
            this.timer = new DispatcherTimer();
            this.timer.Tick += timer_Tick;
            this.timer.Interval = new System.TimeSpan(0, 0, 1);
            this.timer.Start();
        }
        private void timer_Tick(object sender, System.EventArgs e)
        {
            this.pb.Value = System.DateTime.Now.Second % 100;
        }
    }
}

你可以通过改变Value属性来改变进度条的行为(不要忘记在xaml中定义Maximum属性)

我发现了这个(WPF多线程:使用BackgroundWorker并向UI报告进度)。链接)包含一个伟大的解决方案,为我的需求,虽然有一个对话框。

我发现非常有用的一件事是工作线程不能访问主窗口的控件(在它自己的方法中)。但是,在主窗口事件处理程序中使用委托时,这是可能的。

worker.RunWorkerCompleted += delegate(object s, RunWorkerCompletedEventArgs args)
{
    pd.Close();
    // Get a result from the asynchronous worker
    T t = (t)args.Result
    this.ExampleControl.Text = t.BlaBla;
};