一、
<Window x:Class="WpfApp.Windows.ProgressWindow"xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"Title="现代进度条" Height="220" Width="420"WindowStartupLocation="CenterScreen"Background="#F5F6FA" FontFamily="Segoe UI"ResizeMode="NoResize"><Window.Resources><!-- 自定义圆角渐变进度条样式 --><Style x:Key="ModernProgressBar" TargetType="ProgressBar"><Setter Property="SnapsToDevicePixels" Value="True" /><Setter Property="Template"><Setter.Value><ControlTemplate TargetType="ProgressBar"><Grid x:Name="Root" Height="20" ClipToBounds="True" SnapsToDevicePixels="True"><!-- 背景条 --><Border x:Name="PART_Track"Background="#E0E0E0"CornerRadius="10"Height="20" /><!-- 进度条指示 --><Border x:Name="PART_Indicator"Height="20"HorizontalAlignment="Left"CornerRadius="10"Width="0"><Border.Background><LinearGradientBrush StartPoint="0,0" EndPoint="1,0"><GradientStop Color="#0078D7" Offset="0" /><GradientStop Color="#4FC3F7" Offset="1" /></LinearGradientBrush></Border.Background></Border></Grid></ControlTemplate></Setter.Value></Setter></Style></Window.Resources><Grid><StackPanel VerticalAlignment="Center" HorizontalAlignment="Center" Margin="20" Width="360"><TextBlock Text="处理中,请稍候..." FontSize="18" FontWeight="SemiBold" Foreground="#333" Margin="0,0,0,20" HorizontalAlignment="Center" /><!-- 进度条 --><ProgressBar x:Name="progressBar"Height="20"Minimum="0"Maximum="100"Value="0"Style="{StaticResource ModernProgressBar}" /><TextBlock x:Name="progressText" FontSize="14" Foreground="#555" HorizontalAlignment="Center" Margin="0,10,0,0" Text="0%" /><Button Content="开始处理"Width="120" Height="36"Background="#0078D7"Foreground="White"FontWeight="Bold"BorderThickness="0"Cursor="Hand"Margin="0,20,0,0"Click="StartProcessing_Click"HorizontalAlignment="Center" /></StackPanel></Grid> </Window>
二、
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows; using System.Windows.Controls; using System.Windows.Data; using System.Windows.Documents; using System.Windows.Input; using System.Windows.Media; using System.Windows.Media.Imaging; using System.Windows.Shapes;namespace WpfApp.Windows {/// <summary>/// ProgressWindow.xaml 的交互逻辑/// </summary>public partial class ProgressWindow : Window{public ProgressWindow(){InitializeComponent();}private async void StartProcessing_Click(object sender, RoutedEventArgs e){progressBar.Value = 0;for (int i = 0; i <= 100; i++){progressBar.Value = i;progressText.Text = $"{i}%";await Task.Delay(20);}MessageBox.Show("处理完成!", "提示", MessageBoxButton.OK, MessageBoxImage.Information);}} }
