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>
相关推荐
A9better4 分钟前
C++——不一样的I/O工具与名称空间
开发语言·c++·学习
清水白石0087 分钟前
《为什么说 deque 是 Python 滑动窗口的“隐藏神器”?深入解析双端队列的高效之道》
开发语言·python
杜子不疼.7 分钟前
Ascend_C自定义算子开发
c语言·开发语言
WooaiJava23 分钟前
流式TTS音频播放项目 - 面试问答(后端)
java·开发语言
新缸中之脑32 分钟前
开发AI代理必备的8个Python 库
开发语言·人工智能·python
暴走十八步35 分钟前
PHP+vscode开启调试debug
开发语言·vscode·php
郝学胜-神的一滴38 分钟前
Python 列表 vs 数组:深入解析与最佳选择指南
开发语言·python·程序人生
杜子不疼.39 分钟前
基于ATVC模板库的Ascend C Vector算子快速开发指南
c语言·开发语言·mfc
MSTcheng.41 分钟前
【C++】C++11新特性(三)
开发语言·c++·c++11
learning-striving42 分钟前
kali连不上网解决方法
linux·开发语言·网络·php·kali