WPF 进度条

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/Empty_Android/article/details/84997386

委托实现进度条的刷新

实现效果

1. 解决方案

2. 代码

Mainwindow.xaml.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
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 WpfAppProgressBar
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        WindowProgressBar windowProgressBar;
        int progressValue = 1;

        public MainWindow()
        {
            InitializeComponent();
        }

        private void btnRunProgressBar_Click(object sender, RoutedEventArgs e)
        {
            Thread threadExport = new Thread(new ThreadStart(ThreadExport));
            // threadExport.SetApartmentState(ApartmentState.STA);
            threadExport.Start();
        }

        private void ThreadExport()
        {
            System.Windows.Application.Current.Dispatcher.Invoke(new Action(() =>
            {
                WindowProgressBar.Maximum = 3;
                windowProgressBar = new WindowProgressBar();
                windowProgressBar.Owner = this;
                windowProgressBar.WindowStartupLocation = System.Windows.WindowStartupLocation.CenterOwner;
                windowProgressBar.Show();
                windowProgressBar.Activate();
            }));


            int progressValue = 1;

            for (int i = 0; i < 3; i++)
            {
                System.Windows.Application.Current.Dispatcher.Invoke(new Action(() =>
                {
                    Thread.Sleep(1000);
                    windowProgressBar.SetValue(progressValue);

                    progressValue++;
                }));
            }
        }
    }
}

WindowProgressBar.xaml

<Window x:Class="WpfAppProgressBar.WindowProgressBar"
        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:WpfAppProgressBar"
        mc:Ignorable="d"
        Title="WindowProgressBar" Height="190" Width="590" WindowStyle="None">
    <Grid>
        <Grid Height="190" Width="590" Background="Gray">
            <ProgressBar Name="progressBar" Height="42" HorizontalAlignment="Left" Margin="43,68,0,0" VerticalAlignment="Top" Width="490" Background="White"></ProgressBar>
            <Label Content="" Height="39" HorizontalAlignment="Left" Margin="398,128,0,0" Name="labelProgressInfo" VerticalAlignment="Top" Width="135" Foreground="White" FontSize="24" />
        </Grid>
    </Grid>
</Window>

WindowProgressBar.xaml.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
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 WpfAppProgressBar
{
    /// <summary>
    /// Interaction logic for WindowProgressBar.xaml
    /// </summary>
    public partial class WindowProgressBar : Window
    {
        public static int Maximum;
        private delegate void UpdateProgressBarDelegate(System.Windows.DependencyProperty dp, Object value);

        public WindowProgressBar()
        {
            InitializeComponent();

            progressBar.Maximum = Maximum;
            progressBar.Minimum = 0;
            progressBar.Value = 1;
        }

        public void SetValue(int value)
        {
            this.labelProgressInfo.Content = value + "/" + Maximum;

            UpdateProgressBarDelegate updateProgressBarDelegate = new UpdateProgressBarDelegate(progressBar.SetValue);

            Dispatcher.Invoke(updateProgressBarDelegate, System.Windows.Threading.DispatcherPriority.Background, new object[] { System.Windows.Controls.ProgressBar.ValueProperty, Convert.ToDouble(value + 1) });

            if (value == Maximum)
            {
                Thread.Sleep(1000);
                this.Close();
            }
        }
    }
}

猜你喜欢

转载自blog.csdn.net/Empty_Android/article/details/84997386