当 WPF 进度栏中的 Indeterminate="True" 时,如何显示进度?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2844833/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 21:46:27  来源:igfitidea点击:

How do you show progress when Indeterminate="True" in a WPF Progress bar?

wpfwpf-controls

提问by user38349

This has to be the simplest of all questions, but I can not seem to figure it out. I have the progress bar in place, how do I make it show progress. How do I start the thing?

这必须是所有问题中最简单的,但我似乎无法弄清楚。我有进度条,如何让它显示进度。我如何开始这件事?

<ProgressBar x:Name="ProgressUpload" Margin="5" IsIndeterminate="True" ></ProgressBar>

回答by Maurizio Reginelli

If you set IsIndeterminate to True, the progress has the meaning that something is in progress but you cannot determine the exact duration. So, I can only tell you to set it to false and to use the progress bar in its "standard" behavior.

如果您将 IsIndeterminate 设置为 True,则进度表示正在进行某项工作,但您无法确定确切的持续时间。因此,我只能告诉您将其设置为 false 并在其“标准”行为中使用进度条。

回答by dyslexicanaboko

Simply put if you are trying to make the progress bar start, but as an indeterminate bar, then you must set the property IsIndeterminate to true when ready and to false when finished.

简单地说,如果您试图让进度条开始,但作为一个不确定的进度条,那么您必须在准备好时将属性 IsIndeterminate 设置为 true,并在完成时设置为 false。

So in other words:

所以换句话说:

pbar.IsIndeterminate = true; //This starts your bar's animation
pbar.IsIndeterminate = false; //This stops your bar's animation

To give you context as to why you would want to do it this way look at the following psuedo code:

为了让您了解为什么要这样做,请查看以下伪代码:

//Some method that is going to start something that is going to take a while
public void StartLongRunningProcess()
{
    //Make a call to a web service asynchronously etc...

    //Start the animation for your progress bar
    pbar.IsIndeterminate = true;
}

//The method (delegate) that handles the result, usually from an event.
//This method will handle the result of the asynchronous call 
public void HandlerForLongRunningProcess()
{
    //Do stuff with result from your asynchronous web service call etc...

    //Stop the animation for your progress bar
    pbar.IsIndeterminate = false;
}

Let me be the first to say that I am not sure if this is the intended usage of this property, but I can say it definitely works.

让我首先说我不确定这是否是此属性的预期用途,但我可以说它绝对有效。

回答by jmalmari

Apparently in some environments Height has to be set explicitly for indeterminate animation to run, while on others it is not needed.

显然,在某些环境中,必须明确设置 Height 才能运行不确定的动画,而在其他环境中则不需要。

回答by Greg

Don't set it to IsIndeterminate during the initialisation (i.e. UI designer in XAML, or the constructor on the code side) of the window that owns it. If you do then the animation will not start. Set it within the 'Loaded' event handler.

不要在拥有它的窗口的初始化(即 XAML 中的 UI 设计器,或代码端的构造函数)期间将其设置为 IsIndeterminate。如果您这样做,则动画将不会开始。在 'Loaded' 事件处理程序中设置它。

I would have IsIndeterminate = 'False'on the XAML side, and then in the Window_Loadedevent, set:

我会IsIndeterminate = 'False'在 XAML 方面,然后在Window_Loaded事件中设置:

myProgressBar.IsIndeterminate = true;

回答by Stefan

A possible workaround of the problem is to simply show or hide the ProgressBar control:

该问题的一个可能解决方法是简单地显示或隐藏 ProgressBar 控件:

progressBar.Visibility = Visibility.Visible;

progressBar.Visibility = Visibility.Collapsed;

回答by Alan

This is no real difference than @dyslexicanaboko above, but it is quick and easy to do for a demonstration you can control:

这与上面的@dyslexicanaboko 没有真正的区别,但是对于您可以控制的演示,它可以快速轻松地进行:

In XAML:

在 XAML 中:

<Button Content="Start Process" HorizontalAlignment="Center" Click="StartAProcess"/>
<Button Content="Stop Process" HorizontalAlignment="Center" Click="StopAProcess"/>
<ProgressBar Name="pBar1" Height="20"/>

In code behind:

在后面的代码中:

Public Sub StartAProcess()
  pBar1.IsIndeterminate = True
End Sub

Public Sub StopAProcess()
  pBar1.IsIndeterminate = False
End Sub

Upon clicking the Start Process button the animation will start and continue until the Stop Process button is clicked. It should be obvious, but the IsIndeterminate option is not good UI practice; better to actually update the value, but for those who want to see this in action...

单击 Start Process 按钮后,动画将开始并继续,直到单击 Stop Process 按钮。应该很明显,但是 IsIndeterminate 选项不是好的 UI 实践;最好实际更新该值,但对于那些希望看到此操作的人...

回答by Jon9865

Also make sure that CacheMode="BitmapCache" is not set to your page - otherwise the animation will not run. It just displays the cached bitmap...

还要确保 CacheMode="BitmapCache" 未设置为您的页面 - 否则动画将无法运行。它只是显示缓存的位图...