How do you show progress when IsIndeterminate="True" in a WPF progress bar?
This has to be the simplest question of all, but I cannot 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&q开发者_如何学Gouot; IsIndeterminate="True" ></ProgressBar>
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.
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.
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.
Apparently in some environments Height has to be set explicitly for indeterminate animation to run, while on others it is not needed.
A possible workaround of the problem is to simply show or hide the ProgressBar control:
progressBar.Visibility = Visibility.Visible;
progressBar.Visibility = Visibility.Collapsed;
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.
I would have IsIndeterminate = 'False'
on the XAML side, and then in the Window_Loaded
event, set:
myProgressBar.IsIndeterminate = true;
This is no real difference than @dyslexicanaboko above, but it is quick and easy to do for a demonstration you can control:
In 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...
Also make sure that CacheMode="BitmapCache" is not set to your page - otherwise the animation will not run. It just displays the cached bitmap...
In my case (Windows 10, WinUI3, SDK 1.1.0) the animation started when I added the missing Mode=OneWay to the XamlIsIndeterminate variable in the xaml file:
<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>
精彩评论