可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
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); }
load event as below
private void Window_Loaded(object sender, RoutedEventArgs e) { progressBar1.Minimum = 0; progressBar1.Value = DateTime.Now.Second; progressBar1.Maximum = 700; timer.Start(); }
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 i execute my program progressbar shows the progress for two-three bars and then it stops increment. Later there is no effect in the progress at all. What went wrong in my code. Please help!..
Regards Sangeetha
回答1:
In my WPF application I have ... System.Windows.Forms.Timer timer;
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 oher problems too, like blocking the main (dispatcher) thread.
You are only setting the Value once, in the Loaded event:
progressBar1.Value = DateTime.Now.Second;
There is no change to progressBar1.Value in the Tick event. So it figures that it stops moving.
回答2:
Since your ProgressBar doesn't relate to any particular behavior, it looks like a job for an indeterminate bar.
This other SO question provides some insight about it. In short, it's a XAML one-liner:
Then in code, you go like this:
progressBar1.IsIndeterminate = true; // start animation progressBar1.IsIndeterminate = false; // stop animation
回答3:
Use DispatcherTimer instead of Timer (Forms object), and use Value property of ProgressBar.
Try this :
MainWindows.xaml :
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; } } }
You can change the behaviour of the progress bar by changing the Value property (don't forget the Maximum property define in the xaml).
回答4:
I found this (WPF Multithreading: Using the BackgroundWorker and Reporting the Progress to the UI. link) to contain a great solution for my needs, albeit with a Dialog box.
The one thing I found very useful was that the worker thread couldn't access the MainWindow's controls (in it's own method), however when using a delegate inside the main windows event handler it was possible.
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; };