WPF创建进度条

使用wpf做一个原生的进度条,进度条上面有值,先看效果。

功能就是点击按钮,后台处理数据,前台显示处理数据的变化,当然还可以对进度条进行美化和关闭的操作,等待后台处理完毕数据,然后自动关闭。

WPF创建进度条_第1张图片

1.首先简简单单创建一个项目

WPF创建进度条_第2张图片

2.先建立进度条的页面DialogWait.xaml


    

3.DialogWait.xaml.cs后台代码如下

关键点就是要对max的值进行判断,如果大于100和小于100的话,显示是不一样的,主要是因为进度条的值是100,要相对的扩大或者缩小,那么界面上显示的数据变化就是一样的。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
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.Shapes;

namespace WpfApp5
{
    /// 
    /// DialogWait.xaml 的交互逻辑
    /// 
    public partial class DialogWait : Window
    {
        /// 
        /// 进度条最大值
        /// 
        private int max = 0;
        /// 
        /// 大于100的增量
        /// 
        private int increment = 0;
        /// 
        /// 小于100的增量
        /// 
        private double incrementLess = 0;
        public DialogWait()
        {
            InitializeComponent();
        }
        public DialogWait(int max)
        {
            InitializeComponent();
            this.Width = 300;
            this.Height = 25;
            this.Owner = Application.Current.MainWindow;
            this.WindowStartupLocation = WindowStartupLocation.CenterOwner;
            this.max = max;
            if (max >= 100)
            {
                increment = max / 100;
            }
            else
            {
                incrementLess = Math.Round(100.0 / max, 3);
            }
        }
        public void ShowWait(int value)
        {
            progressBar.Dispatcher.Invoke(() =>
            {
                if (max >= 100)
                {
                    progressBar.Value = value / increment;
                }
                else
                {
                    progressBar.Value = Math.Round(value * incrementLess, 0);
                }
            });
        }
    }
}

4.MainWindow.xaml.cs的代码

其中最重要的就是Task

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
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 WpfApp5
{
    /// 
    /// Interaction logic for MainWindow.xaml
    /// 
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            DialogWait dialogWaitPercent = new DialogWait(500);
            Task.Run(() =>
            {
                A(dialogWaitPercent);//处理数据
            });
            dialogWaitPercent.ShowDialog();
        }
        public void A(DialogWait dialogWaitPercent)
        {
            for (int i = 0; i < 500; i++)
            {
                dialogWaitPercent.ShowWait(i);
                Thread.Sleep(10);
            }
            dialogWaitPercent.Dispatcher.Invoke(() =>
            {
                dialogWaitPercent.Close();
            });
        }
    }
}

5.ProgressBarStyle.xaml,最后就是对进度条的美化样式


    

本案例代码:https://download.csdn.net/download/u012563853/88578158

来源:WPF创建进度条-CSDN博客

你可能感兴趣的:(WPF,wpf)