中国建设网站简州新城土地整改项目,长春网络公司,做app需要多少钱,wordpress 设置分页显示条数文章目录 1. ProgressBar控件简介2. ProgressBar的基本属性和用法2.1 基本属性2.2 基本用法2.3 代码中修改进度 3. 确定与不确定模式3.1 确定模式#xff08;Determinate#xff09;3.2 不确定模式#xff08;Indeterminate#xff09; 4. 在多线程环境中更新ProgressBar4.… 文章目录 1. ProgressBar控件简介2. ProgressBar的基本属性和用法2.1 基本属性2.2 基本用法2.3 代码中修改进度 3. 确定与不确定模式3.1 确定模式Determinate3.2 不确定模式Indeterminate 4. 在多线程环境中更新ProgressBar4.1 使用Dispatcher4.2 使用BackgroundWorker4.3 使用Task和ProgressT 5. 自定义ProgressBar外观样式5.1 基本样式设置5.2 使用样式资源5.3 带文本的进度条5.4 动画效果 6. 使用MVVM模式与ProgressBar6.1 ViewModel示例6.2 XAML绑定 7. 实际应用案例7.1 文件下载进度条7.2 批量处理进度显示7.3 带动画的加载指示器 8. 最佳实践8.1 性能考虑8.2 用户体验提示 9. 总结10. 学习资源 可以根据Github拉取示例程序运行 GitHub程序演示地址点击直达 也可以在本文资源中下载 1. ProgressBar控件简介
ProgressBar进度条是WPF中常用的用户界面控件主要用于向用户展示操作的进度或者任务的完成情况。无论是文件下载、数据处理、长时间的计算操作还是需要让用户了解当前进度的任何场景ProgressBar都是理想的选择。
WPF中的ProgressBar控件提供了丰富的功能和高度的可定制性它可以
显示确定性进度具体的百分比进度显示不确定性进度无法预估完成时间的操作通过样式和模板完全自定义外观支持动画和视觉效果与MVVM模式无缝集成
本文将详细介绍ProgressBar控件的基本属性、使用方法、自定义样式以及在实际项目中的应用技巧。
2. ProgressBar的基本属性和用法
2.1 基本属性
ProgressBar控件继承自RangeBase类因此拥有以下重要属性
属性名类型描述Minimumdouble进度条的最小值默认为0Maximumdouble进度条的最大值默认为100Valuedouble当前进度值IsIndeterminatebool是否为不确定模式默认为falseOrientationOrientation进度条的方向水平或垂直ForegroundBrush进度条填充颜色BackgroundBrush进度条背景颜色
2.2 基本用法
下面是一个简单的ProgressBar控件XAML示例
ProgressBar Width200 Height20 Minimum0 Maximum100 Value75 ForegroundGreen/这段代码创建了一个宽度为200高度为20的进度条最小值为0最大值为100当前值为75填充颜色为绿色。
2.3 代码中修改进度
在C#代码中可以通过设置Value属性来更新进度条的进度
// 将进度设置为指定值
progressBar.Value 50;// 增加进度值
progressBar.Value 10;// 检查是否完成
if (progressBar.Value progressBar.Maximum)
{// 处理完成逻辑
}3. 确定与不确定模式
ProgressBar控件有两种工作模式确定模式和不确定模式。
3.1 确定模式Determinate
当您知道任务的总量并能够计算出完成百分比时应该使用确定模式。在这种模式下进度条会显示完成的比例。
ProgressBar Width200 Height20 Minimum0 Maximum100 Value45 IsIndeterminateFalse/3.2 不确定模式Indeterminate
当无法估计任务的完成时间或无法计算进度百分比时应该使用不确定模式。在这种模式下进度条会显示一个动画表示任务正在进行中但没有具体的完成百分比。
ProgressBar Width200 Height20 IsIndeterminateTrue/不确定模式下的进度条会显示一个来回移动的动画让用户知道程序正在工作但不显示具体的完成进度。
4. 在多线程环境中更新ProgressBar
在WPF应用程序中UI元素包括ProgressBar只能在创建它们的线程上更新。当在后台线程执行长时间运行的任务时需要使用特殊方法来更新UI上的ProgressBar。
4.1 使用Dispatcher
// 假设在后台线程中执行任务
void WorkerMethod()
{for (int i 0; i 100; i){// 通过Dispatcher更新UIDispatcher.Invoke(() {progressBar.Value i;});// 模拟工作Thread.Sleep(100);}
}4.2 使用BackgroundWorker
BackgroundWorker是一个更便捷的方式来实现后台处理并更新UI
private void StartProcess()
{BackgroundWorker worker new BackgroundWorker();worker.WorkerReportsProgress true;worker.DoWork Worker_DoWork;worker.ProgressChanged Worker_ProgressChanged;worker.RunWorkerCompleted Worker_RunWorkerCompleted;worker.RunWorkerAsync();
}private void Worker_DoWork(object sender, DoWorkEventArgs e)
{for (int i 0; i 100; i){// 执行耗时操作Thread.Sleep(100);// 报告进度(sender as BackgroundWorker).ReportProgress(i);}
}private void Worker_ProgressChanged(object sender, ProgressChangedEventArgs e)
{// 更新进度条progressBar.Value e.ProgressPercentage;
}private void Worker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{// 处理完成后的逻辑MessageBox.Show(处理完成);
}4.3 使用Task和Progress
在现代WPF应用程序中推荐使用Task和IProgress来实现后台处理和进度报告
private async void StartProcessAsync()
{// 创建进度报告器var progress new Progressint(value {progressBar.Value value;});// 异步执行任务await Task.Run(() ProcessDataWithProgress(progress));MessageBox.Show(处理完成);
}private void ProcessDataWithProgress(IProgressint progress)
{for (int i 0; i 100; i){// 执行耗时操作Thread.Sleep(100);// 报告进度progress.Report(i);}
}5. 自定义ProgressBar外观样式
WPF的强大之处在于它提供了丰富的样式和模板定制能力ProgressBar控件同样可以完全自定义外观。
5.1 基本样式设置
ProgressBar Width200 Height20 Value75ProgressBar.ForegroundLinearGradientBrush StartPoint0,0 EndPoint1,0GradientStop ColorLightBlue Offset0/GradientStop ColorBlue Offset1//LinearGradientBrush/ProgressBar.ForegroundProgressBar.BackgroundSolidColorBrush ColorLightGray//ProgressBar.Background
/ProgressBar5.2 使用样式资源
Window.ResourcesStyle x:KeyCustomProgressBar TargetTypeProgressBarSetter PropertyForeground ValueOrange/Setter PropertyBackground Value#EEEEEE/Setter PropertyHeight Value10/Setter PropertyTemplateSetter.ValueControlTemplate TargetTypeProgressBarGridBorder Background{TemplateBinding Background}BorderBrushGrayBorderThickness1CornerRadius5/Border x:NamePART_IndicatorBackground{TemplateBinding Foreground}BorderBrushDarkOrangeBorderThickness1CornerRadius5HorizontalAlignmentLeft//Grid/ControlTemplate/Setter.Value/Setter/Style
/Window.Resources!-- 使用自定义样式 --
ProgressBar Width200 Value75 Style{StaticResource CustomProgressBar}/5.3 带文本的进度条
WPF的ProgressBar默认不显示文本但我们可以通过叠加控件来实现带有文本显示的进度条
GridProgressBar x:NameprogressBar Width200 Height25 Value45/TextBlock Text{Binding Value, ElementNameprogressBar, StringFormat{}{0:0}%}HorizontalAlignmentCenterVerticalAlignmentCenter/
/Grid5.4 动画效果
可以添加动画效果使ProgressBar更具视觉吸引力
Style x:KeyAnimatedProgressBar TargetTypeProgressBarSetter PropertyTemplateSetter.ValueControlTemplate TargetTypeProgressBarGridBorder Background{TemplateBinding Background}BorderBrushGrayBorderThickness1CornerRadius5/Border x:NamePART_IndicatorBackground{TemplateBinding Foreground}BorderThickness0CornerRadius5HorizontalAlignmentLeftBorder.EffectDropShadowEffect ColorBlue ShadowDepth0 BlurRadius10//Border.Effect/Border/Grid/ControlTemplate/Setter.Value/SetterStyle.TriggersEventTrigger RoutedEventProgressBar.ValueChangedBeginStoryboardStoryboardColorAnimation Storyboard.TargetProperty(Border.Effect).(DropShadowEffect.Color)ToLightBlue Duration0:0:0.5 AutoReverseTrue//Storyboard/BeginStoryboard/EventTrigger/Style.Triggers
/Style6. 使用MVVM模式与ProgressBar
在MVVMModel-View-ViewModel架构中ProgressBar通常绑定到ViewModel中的属性而不是直接在代码后台操作。
6.1 ViewModel示例
public class MainViewModel : INotifyPropertyChanged
{private double _progress;public double Progress{get { return _progress; }set{if (_progress ! value){_progress value;OnPropertyChanged(nameof(Progress));}}}public ICommand StartProcessCommand { get; }public MainViewModel(){StartProcessCommand new RelayCommand(ExecuteStartProcess);}private async void ExecuteStartProcess(){Progress 0;for (int i 0; i 100; i){await Task.Delay(100); // 模拟工作Progress i;}}public event PropertyChangedEventHandler PropertyChanged;protected void OnPropertyChanged(string propertyName){PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));}
}6.2 XAML绑定
Window x:ClassWpfApp.MainWindowxmlnshttp://schemas.microsoft.com/winfx/2006/xaml/presentationxmlns:xhttp://schemas.microsoft.com/winfx/2006/xamlxmlns:localclr-namespace:WpfAppTitleProgressBar Demo Height200 Width400Window.DataContextlocal:MainViewModel//Window.DataContextStackPanel Margin20ProgressBar Height20 Minimum0 Maximum100 Value{Binding Progress}/TextBlock Text{Binding Progress, StringFormat进度: {0:0}%}Margin0,10,0,10/Button Content开始处理 Command{Binding StartProcessCommand} Width100 HorizontalAlignmentLeft//StackPanel
/Window7. 实际应用案例
7.1 文件下载进度条
private async void DownloadFile(string url, string destination)
{using (WebClient client new WebClient()){client.DownloadProgressChanged (sender, e) {progressBar.Value e.ProgressPercentage;statusText.Text $下载进度: {e.ProgressPercentage}% - 已下载 {e.BytesReceived / 1024} KB / 共 {e.TotalBytesToReceive / 1024} KB;};client.DownloadFileCompleted (sender, e) {if (e.Error null)statusText.Text 文件下载完成;elsestatusText.Text $下载出错: {e.Error.Message};};await client.DownloadFileTaskAsync(new Uri(url), destination);}
}7.2 批量处理进度显示
private async Task ProcessFilesAsync(string[] files)
{progressBar.Minimum 0;progressBar.Maximum files.Length;progressBar.Value 0;for (int i 0; i files.Length; i){statusText.Text $正在处理: {System.IO.Path.GetFileName(files[i])};await Task.Run(() ProcessSingleFile(files[i]));progressBar.Value i 1;}statusText.Text 所有文件处理完成;
}private void ProcessSingleFile(string filePath)
{// 实际的文件处理逻辑Thread.Sleep(1000); // 模拟耗时操作
}7.3 带动画的加载指示器
GridGrid.ResourcesStyle x:KeyFancyProgressBar TargetTypeProgressBarSetter PropertyTemplateSetter.ValueControlTemplate TargetTypeProgressBarGrid x:NameTemplateRootBorder Background{TemplateBinding Background} BorderBrush{TemplateBinding BorderBrush} BorderThickness{TemplateBinding BorderThickness}CornerRadius10/Grid x:NamePART_TrackRectangle x:NamePART_Indicator Fill{TemplateBinding Foreground} HorizontalAlignmentLeft RadiusX10 RadiusY10//Grid/GridControlTemplate.TriggersTrigger PropertyIsIndeterminate ValuetrueSetter TargetNamePART_Indicator PropertyWidth Value30/Setter TargetNamePART_Indicator PropertyHorizontalAlignment ValueLeft/Trigger.EnterActionsBeginStoryboardStoryboard RepeatBehaviorForeverDoubleAnimationUsingKeyFrames Storyboard.TargetNamePART_Indicator Storyboard.TargetProperty(UIElement.RenderTransform).(TransformGroup.Children)[3].(TranslateTransform.X)SplineDoubleKeyFrame KeyTime0:0:0 Value-30/SplineDoubleKeyFrame KeyTime0:0:1.5 Value300//DoubleAnimationUsingKeyFrames/Storyboard/BeginStoryboard/Trigger.EnterActions/Trigger/ControlTemplate.Triggers/ControlTemplate/Setter.Value/SetterSetter PropertyForeground Value#FF1BA1E2/Setter PropertyBackground Value#FFE6E6E6/Setter PropertyBorderBrush Value#FFADADAD/Setter PropertyBorderThickness Value1/Setter PropertyMinHeight Value10//Style/Grid.ResourcesProgressBar Style{StaticResource FancyProgressBar} IsIndeterminateTrue Height20 Width200/
/Grid8. 最佳实践
8.1 性能考虑
避免过于频繁地更新进度条可以考虑每隔一定时间间隔更新一次在处理大量数据时可以考虑使用批处理更新进度使用异步方法和Task而不是Thread可以更好地保持UI响应性
// 优化的进度更新
private async Task ProcessLargeDataAsync(ListDataItem items)
{const int batchSize 100; // 每100项更新一次进度int count 0;foreach (var item in items){await ProcessItemAsync(item);count;if (count % batchSize 0 || count items.Count){double progress (double)count / items.Count * 100;progressBar.Value progress;await Task.Delay(1); // 允许UI更新}}
}8.2 用户体验提示
对于预计需要较长时间的操作提供取消选项添加文本反馈让用户了解具体进度和剩余时间对于已完成的进度条考虑添加成功/失败的视觉提示
private async void StartLongOperation(CancellationTokenSource cts)
{try{DateTime startTime DateTime.Now;int totalItems 1000;for (int i 0; i totalItems; i){// 检查取消请求if (cts.Token.IsCancellationRequested){statusText.Text 操作已取消;return;}await Task.Delay(10, cts.Token);// 更新进度和时间估计double progress (double)(i 1) / totalItems;progressBar.Value progress * 100;// 计算预估剩余时间if (i 0){TimeSpan elapsed DateTime.Now - startTime;TimeSpan estimated TimeSpan.FromTicks((long)(elapsed.Ticks / progress));TimeSpan remaining estimated - elapsed;statusText.Text $进度: {progress:P0} - 预计剩余时间: {(int)remaining.TotalMinutes}分{remaining.Seconds}秒;}}// 成功完成progressBar.Foreground Brushes.Green;statusText.Text 操作成功完成;}catch (OperationCanceledException){statusText.Text 操作已取消;}catch (Exception ex){// 处理失败progressBar.Foreground Brushes.Red;statusText.Text $操作失败: {ex.Message};}
}9. 总结
ProgressBar是WPF应用程序中非常实用的控件它能直观地向用户展示操作进度提高用户体验。通过本文的学习我们了解了
ProgressBar控件的基本属性和用法确定模式和不确定模式的区别和应用场景如何在多线程环境中正确更新ProgressBar自定义ProgressBar外观的多种方法在MVVM架构中使用ProgressBar的最佳实践实际应用案例和优化技巧
通过掌握ProgressBar控件的使用您可以大幅提升WPF应用程序的用户体验让用户清楚了解操作的进度减少等待过程中的不确定感。
10. 学习资源
Microsoft WPF ProgressBar官方文档ProgressBar样式和模板指南WPF多线程编程指南WPF社区资源