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>
相关推荐
小灰灰搞电子3 分钟前
Rust可以取代C++么?
开发语言·c++·rust
cat三三7 分钟前
java之异常
java·开发语言
奇树谦9 分钟前
【Qt实战】实现图片缩放、平移与像素级查看功能
开发语言·qt
Lv117700810 分钟前
Visual Studio中的多态
ide·笔记·c#·visual studio
我命由我1234516 分钟前
Python Flask 开发问题:ImportError: cannot import name ‘Markup‘ from ‘flask‘
开发语言·后端·python·学习·flask·学习方法·python3.11
wjs202419 分钟前
Go 语言指针
开发语言
wuguan_31 分钟前
C#:多态函数重载、态符号重载、抽象、虚方法
开发语言·c#
小信啊啊31 分钟前
Go语言数组与切片的区别
开发语言·后端·golang
计算机学姐44 分钟前
基于php的摄影网站系统
开发语言·vue.js·后端·mysql·php·phpstorm