WPF轻松实现进度条的示例代码
作者:小码编匠
WPF中的ProgressBar控件用于表示任务进度,适用于文件下载、数据处理等场景,本文将通过XAML和C#代码展示如何创建一个基本的WPF进度条,并演示如何通过事件处理程序更新进度条的值来模拟耗时操作,感兴趣的小伙伴跟着小编一起来看看吧
前言
本文讲述如何在软件启动和窗体按钮操作时弹出进度条。
运行环境:Win10、VS2022
一、新建WPF项目
二、新建WPF窗体。
1、新建窗体,取名DefProcessBar.xaml。
2、设置窗体属性、样式。
<Window x:Class="WpfApp4.DefProcessBar" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:local="clr-namespace:WpfApp4" mc:Ignorable="d" Title="DefProcessBar" Width="300" Height="50" WindowStartupLocation="CenterScreen" WindowStyle="None" AllowsTransparency="True" Background="Transparent" ShowInTaskbar="False" ResizeMode="NoResize">
2.1、WindowStartupLocation="CenterScreen"是窗体在屏幕中间显示;
2.2、WindowStyle="None"是不要窗体顶部内容;
2.3、AllowsTransparency="True"是允许窗体透明;
2.4、Background="Transparent"是设置透明背景;
2.5、ShowInTaskbar="False"是指示此窗口是否出现在任务栏;
2.6、ResizeMode="NoResize"是不可调节窗体大小。
3、添加进度消息和进度条。
<Grid.RowDefinitions> <RowDefinition Height="25"></RowDefinition> <RowDefinition Height="25"></RowDefinition> </Grid.RowDefinitions> <TextBlock Grid.Row="0" Name="txtTitle" FontSize="16" Foreground="Yellow" HorizontalAlignment="Left" TextWrapping="Wrap" Margin="10, 0,0,0"></TextBlock> <ProgressBar Grid.Row="1" x:Name="progressBar" Maximum="100" Height="25" Width="300" Foreground="Green" Background="LightGray" HorizontalContentAlignment="Center" VerticalContentAlignment="Center"/>
4、添加 DefProcessBar.xaml.cs 代码
/// <summary> /// DefProcessBar.xaml 的交互逻辑 /// </summary> public partial class DefProcessBar : Window { public DefProcessBar() { InitializeComponent(); } public void ShowProcess(int process, string title = "") { progressBar.Dispatcher.Invoke(() => { this.txtTitle.Text = title; this.progressBar.Value = process; }); } public void SetProcess(DefProcessBar defProcessBar, int process, string title = "") { if (process > 100) { process = 100; } else if (process < 0) { process = 0; } title += $" ({process}%)"; defProcessBar.ShowProcess(process, title); } }
三、在 App.xaml.cs 中使用
/// <summary> /// App.xaml 的交互逻辑 /// </summary> public partial class App : Application { protected override void OnStartup(StartupEventArgs e) { Application.Current.ShutdownMode = System.Windows.ShutdownMode.OnExplicitShutdown; bool isFinish = false; //DialogWait defProcessBar = new DialogWait(); DefProcessBar defProcessBar = new DefProcessBar(); Task.Run(() => { defProcessBar.SetProcess(defProcessBar, 20, "启动中,请稍后..."); // 写入实际处理代码 Thread.Sleep(500); defProcessBar.SetProcess(defProcessBar, 40, "启动中,请稍后..."); Thread.Sleep(500); defProcessBar.SetProcess(defProcessBar, 60, "启动中,请稍后..."); Thread.Sleep(500); defProcessBar.SetProcess(defProcessBar, 80, "启动中,请稍后..."); Thread.Sleep(500); defProcessBar.Dispatcher.Invoke(() => { defProcessBar.Close(); }); }); defProcessBar.ShowDialog(); if (isFinish) { base.OnStartup(e); Application.Current.ShutdownMode = ShutdownMode.OnMainWindowClose; } else { this.Shutdown(); } } }
启动软件查看效果。
四、在主窗体 MainWindow 中弹出进度条。
1、添加按钮。
2、添加按钮事件处理。
private void Button_Click(object sender, RoutedEventArgs e) { DefProcessBar defProcessBar = new DefProcessBar(); Task.Run(() => { defProcessBar.SetProcess(defProcessBar, 20, "启动中,请稍后..."); Thread.Sleep(500); defProcessBar.SetProcess(defProcessBar, 40, "启动中,请稍后..."); Thread.Sleep(500); defProcessBar.SetProcess(defProcessBar, 60, "启动中,请稍后..."); Thread.Sleep(500); defProcessBar.SetProcess(defProcessBar, 80, "启动中,请稍后..."); //写入实际处理代码 Thread.Sleep(500); defProcessBar.Dispatcher.Invoke(() => { defProcessBar.Close(); }); }); defProcessBar.ShowDialog(); }
3、效果跟软件启动时一样。
缺点:所有逻辑必须放到异步处理。
最后
以上就是WPF轻松实现进度条的示例代码的详细内容,更多关于WPF实现进度条的资料请关注脚本之家其它相关文章!