64.进度条 C#例子 WPF例子

进度条是比较简单,前台一个进度条,后台给value赋值0到100就可以显示进度了。

完整代码:

cs 复制代码
using System.Text;
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.Navigation;
using System.Windows.Shapes;

namespace 进度条
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }

        private async void Button_Click(object sender, RoutedEventArgs e)
        {
            Button button = (Button)sender;
            button.IsEnabled = false;
            await WaitAsync();
            button.IsEnabled = true;

        }

        public async Task WaitAsync()
        {
            for(int i = 0; i <= 100; i++)
            {
                await Task.Delay(50);
                ProgressBar1.Value = i;
                 使用调度器在UI线程上更新进度条,不过在这里不使用Dispatcher.Invoke也可以
                //System.Windows.Application.Current.Dispatcher.Invoke(() =>
                //{
                //    progressBar.Value = i; // 更新进度条的值
                //});
            }
        }
    }
}
XML 复制代码
<Window x:Class="进度条.MainWindow"
        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:进度条"
        mc:Ignorable="d"
        Title="MainWindow" Height="450" Width="800">
    <Grid>
        <ProgressBar x:Name="ProgressBar1" HorizontalAlignment="Left" Height="10" Margin="287,98,0,0" VerticalAlignment="Top" Width="100"/>
        <Button Content="Button" HorizontalAlignment="Left" Margin="287,133,0,0" VerticalAlignment="Top" Click="Button_Click"/>

    </Grid>
</Window>
相关推荐
caimouse7 分钟前
ReactOS 窗口系统分析(14):计时器/属性/加速键/热键 — timer.c + prop.c + accelerator.c + hotkey.c
c语言·开发语言·reactos
circuitsosk14 分钟前
Python 模块与包管理:import 机制、虚拟环境与 pip 完全指南
开发语言·python·pip·依赖管理·模块与包
就叫飞六吧14 分钟前
两道门:X-Frame-Options 和 SameSite 到底谁管什么
开发语言·chrome·ai编程
fl17683129 分钟前
基于C#WPF实现的内存加速球清理类似360加速球内存清理
开发语言·c#·wpf
weixin_4407305033 分钟前
python+request实现接口-小结
开发语言·python
ZJU_统一阿萨姆1 小时前
【算子开发】扫描(Scan)与前缀和
开发语言·arm开发·架构·系统架构·硬件架构
-凌凌漆-1 小时前
【C语言】结构体typedef struct与struct的区别
c语言·开发语言
李高钢1 小时前
【WPF】高级 UI 与性能优化实战:从卡顿到丝滑
ui·性能优化·wpf
for_ever_love__1 小时前
PySpark学习: 数据输出
开发语言·python·学习·spark
言乐62 小时前
Python实现自动拉人脚本示例
开发语言·windows·python·django·pygame