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>
相关推荐
唐青枫19 小时前
别再乱用 StartNew:C#.NET TaskFactory 任务调度实战详解
c#·.net
Artech1 天前
[MAF预定义的AIContextProvider-03]ChatHistoryMemoryProvider——赋予Agent从经验中学习的能力
ai·c#·agent·memory·maf
Scout-leaf3 天前
C#摸鱼实录——IoC与DI案例详解
c#
咕白m6253 天前
使用 C# 在 Excel 中应用多种字体样式
后端·c#
Artech3 天前
[MAF预定义的AIContextProvider-02]AgentSkillsProvider——将Agent Skills引入MAF
ai·c#·agent·agent skills·maf
LDR0064 天前
Type-C 快充全面升级!LDR6601 赋能个人护理便携电机,重塑剃须刀 / 理发器新体验
c语言·开发语言
雪碧聊技术4 天前
Tree.js是什么?一文讲透
开发语言·javascript·ecmascript
码云数智-园园4 天前
C++20 Modules 模块详解
java·开发语言·spring
swordbob4 天前
NIO的channel中什么是 fd(File Descriptor,文件描述符)
java·开发语言·nio
源分享4 天前
Java线程同步的多种实现方法(非常详细)
java·开发语言·jvm