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>
相关推荐
xiaoye-duck32 分钟前
《C++ 项目:手写大模型接入 SDK》LLMManager架构设计与实现
开发语言·网络·c++·语言模型
一条大祥脚1 小时前
CuTe 与 CuTe DSL:从 C++ 模板元编程到 Python DSL 的 GPU 编程演进
开发语言·c++·python·cutlass·cute·cute dsl
FRing032 小时前
vue2干货总结8(路由模块封装、router-link、声明式导航、路由重定向、编程式导航)
开发语言·前端·vue.js·vue2
旖旎夜光2 小时前
【C++ 基础入门】C++ 多态(上):虚函数、重写、override/final 与抽象类
开发语言·c++·笔记·学习
starzy19902 小时前
深入理解Scala嵌套函数:作用域封装之道
开发语言·scala
Sakura_j_h_2 小时前
从零写一个 MCP stdio Server:用 Python 校验博客文章格式
开发语言·python·自动化·ai工具·mcp·json-rpc
白杨尚青4 小时前
C++进阶篇(五):红黑树——四条规则、三种插入情况与旋转变色
开发语言·数据结构·c++·stl·红黑树·平衡二叉树
.道阻且长.4 小时前
Linux基本开发工具:gdb和cgdb的使用
linux·运维·服务器·开发语言
starzy19905 小时前
深入理解Scala匿名函数:函数式编程基石
开发语言·scala
郝学胜-神的一滴5 小时前
AI 编程智能体 27:吃透MCP通信机制
人工智能·python·langchain·wpf·numpy·scikit-learn