【问题标题】:How do you show progress when IsIndeterminate="True" in a WPF progress bar?当 WPF 进度条中的 IsIndeterminate="True" 时,如何显示进度?
【发布时间】:2021-10-23 17:07:05
【问题描述】:

这一定是最简单的问题,但我似乎无法弄清楚。我有进度条。如何让它显示进度?我该如何开始?

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

【问题讨论】:

  • 一开始为什么会有IsIndeterminate="True"

标签: wpf wpf-controls


【解决方案1】:

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

【讨论】:

  • 我要解决的问题是如何让它开始动画。我已经定义了栏并且是 IsIndeterminate="True"。如何触发栏做它的事情?
  • 我一直在寻找的技巧是我必须设置一个最小值和一个最大值以及一个高度。一旦我设置了这些,它就会显示出来。
【解决方案2】:

简单地说,如果你试图让进度条开始,但作为一个不确定的条,那么你必须在准备好时将属性 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;
}

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

【讨论】:

    【解决方案3】:

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

    【讨论】:

      【解决方案4】:

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

      progressBar.Visibility = Visibility.Visible;
      
      progressBar.Visibility = Visibility.Collapsed;
      

      【讨论】:

        【解决方案5】:

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

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

        myProgressBar.IsIndeterminate = true;
        

        【讨论】:

          【解决方案6】:

          这与上面的@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 选项不是好的 UI 实践;更好地实际更新值,但对于那些希望看到这一点的人......

          【讨论】:

            【解决方案7】:

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

            【讨论】:

              猜你喜欢
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 2023-03-10
              相关资源
              最近更新 更多