C# WPF 进度条在几个条后停止
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15429256/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
WPF Progressbar Stops after a Few Bars
提问by Sangeetha
In my WPF application i have to show a progressbar progress with in a timer tick event, which i am writing as below,
在我的 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);
}
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 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.
当程序的进度条显示两到三个条的进度时,它会停止增加。后来的进度完全没有影响。
Why?
为什么?
采纳答案by Henk Holterman
In my WPFapplication I have ...
System.Windows.Forms.Timer timer;
在我的WPF应用程序中,我有...
System.Windows.Forms.Timer timer;
That is the wrong type of timer. Use a DispatcherTimerinstead.
那是错误类型的计时器。请改用DispatcherTimer。
When i execute my program progressbar shows the progress for two-three bars and then it stops
当我执行我的程序时,progressbar 会显示 2-3 bar 的进度,然后它会停止
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:
您只需在 Loaded 事件中设置一次值:
progressBar1.Value = DateTime.Now.Second;
There is no change to progressBar1.Value
in the Tick event. So it figures that it stops moving.
progressBar1.Value
Tick 事件中没有更改。所以它认为它停止移动。
回答by Alex
Since your ProgressBar
doesn't relate to any particular behavior, it looks like a job for an indeterminatebar.
由于您ProgressBar
与任何特定行为无关,因此它看起来像是不确定酒吧的工作。
This other SO questionprovides some insight about it. In short, it's a XAML one-liner:
这个另一个 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"/>
Then in code, you go like this:
然后在代码中,你是这样的:
progressBar1.IsIndeterminate = true; // start animation
progressBar1.IsIndeterminate = false; // stop animation
回答by Xaruth
Use DispatcherTimer instead of Timer (Forms object), and use Value property of ProgressBar.
使用 DispatcherTimer 代替 Timer(Forms 对象),并使用 ProgressBar 的 Value 属性。
Try this :
尝试这个 :
MainWindows.xaml :
主窗口.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 :
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).
您可以通过更改 Value 属性来更改进度条的行为(不要忘记在 xaml 中定义的 Maximum 属性)。
回答by lko
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.
我发现这个(WPF 多线程:使用 BackgroundWorker 和向 UI 报告进度。链接)包含一个很好的解决方案,可以满足我的需求,尽管有一个对话框。
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.
我发现非常有用的一件事是工作线程无法访问 MainWindow 的控件(在它自己的方法中),但是当在主窗口事件处理程序中使用委托时,这是可能的。
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;
};