当WPF进度条的IsIndeterminate属性设置为“True”时,如何显示进度?

13

这应该是最简单的问题,但我似乎无法弄清楚。我已经放置了进度条。如何显示进度?我该如何启动它?

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

3
你为什么一开始就要使用 IsIndeterminate="True" - H H
8个回答

25

如果将IsIndeterminate设置为True,则进度意味着某些内容正在进行中,但你无法确定确切的持续时间。因此,我只能告诉你将其设置为false,并在进度条的“标准”行为中使用它。


1
我正在解决的问题是如何启动该动画。我已经定义了进度条,并将IsIndeterminate设置为“True”。那么,我该如何触发进度条开始工作呢? - user38349
10
我需要设定一个最小值、最大值和高度,这样才能显示出我想要的效果。 - user38349

17

简单来说,如果您想让进度条开始运行,但是作为一个不确定的进度条,那么当准备好时,必须将IsIndeterminate属性设置为true,在完成时将其设置为false。

换句话说:

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

为了让你明白为什么要这样做,请看以下伪代码:

//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;
}

我先说一下,我不确定这个属性的预期用法是什么,但我可以肯定地说它确实有效。


1
问题的一个可能解决方法是简单地显示或隐藏ProgressBar控件:
progressBar.Visibility = Visibility.Visible;

progressBar.Visibility = Visibility.Collapsed;

1

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


0
不要在拥有它的窗口的初始化期间(即XAML中的UI设计师或代码侧的构造函数)将其设置为IsIndeterminate。如果这样做,动画将无法启动。请在“Loaded”事件处理程序中设置它。
我会在XAML侧设置IsIndeterminate = 'False',然后在Window_Loaded事件中设置:
myProgressBar.IsIndeterminate = true;

0
在我的情况下(Windows 10,WinUI3,SDK 1.1.0),当我在Xaml文件中添加了缺失的Mode=OneWay到XamlIsIndeterminate变量时,动画开始了:
  <Grid Margin="10, 20">
                <ProgressBar Name="pbStatus" BorderThickness="1" Foreground="Green" 
                        Value="{x:Bind Path=XamlProductionViewModel.XamlCurrentProgress, Mode=OneWay}" 
                        IsIndeterminate="{x:Bind Path=XamlProductionViewModel.XamlIsIndeterminate, Mode=OneWay}"
                        Visibility="{x:Bind Path=XamlProductionViewModel.XamlProgressVisibility, Converter={StaticResource booleanToVisibilityConverter}, Mode=OneWay}" 
                        Height="30" Background="Aqua" Minimum="0" Maximum="100"/>
  </Grid>

0
同时确保您的页面未设置CacheMode="BitmapCache",否则动画将无法运行。它只会显示缓存的位图...

0

这与上面的@dyslexicanaboko没有实质性区别,但对于您可以控制的演示而言,这是快速且易于完成的:

在XAML中:

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

在代码后端:
Public Sub StartAProcess()
  pBar1.IsIndeterminate = True
End Sub

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

点击“开始进程”按钮后,动画将开始并持续到单击“停止进程”按钮。这很明显,但IsIndeterminate选项不是良好的用户界面实践;最好实际更新值,但对于那些想要看到它实际运作的人...

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接