c# ProgressBar 委托刷新

Solution Explorer


WinProgressBar.xaml

<Window x:Class="WpfApplicationProgressBarDemo.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:WpfApplicationProgressBarDemo"
        mc:Ignorable="d"
        Title="WindowProgressBar" Height="90" Width="700" WindowStyle="None">
    <Grid>
        <ProgressBar Name="progressBarDeleteRecords" Background="Gray" Foreground="DodgerBlue"></ProgressBar>
        <Label Name="labelDeleteRecordsInfo" Height="40" Width="200" FontSize="30" Foreground="White" HorizontalAlignment="Center" VerticalAlignment="Center"></Label>
    </Grid>
</Window>

WinProgressBar.xaml.cs

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.Shapes;

namespace WpfApplicationProgressBarDemo
{
    /// <summary>
    /// Interaction logic for WindowProgressBar.xaml
    /// </summary>

    delegate void UpdateDelegate(System.Windows.DependencyProperty dp, Object value);

    public partial class WindowProgressBar : Window
    {
        public int ProgressBarMaxValue { get; set; }

        public WindowProgressBar(int ProgressBarMaxValue)
        {
            InitializeComponent();

            this.ProgressBarMaxValue = ProgressBarMaxValue;
            progressBarDeleteRecords.Maximum = ProgressBarMaxValue;
            progressBarDeleteRecords.Minimum = 0;
            progressBarDeleteRecords.Value = 1;
        }

        public void SetProgressBarValue(int Value)
        {
            this.labelDeleteRecordsInfo.Content = Value + "/" + ProgressBarMaxValue;

            UpdateDelegate updateProgressBarDelegate = new UpdateDelegate(progressBarDeleteRecords.SetValue);

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

            if (Value == ProgressBarMaxValue)
            {
                Thread.Sleep(1000);

                ProgressBarMaxValue = 0;

                this.Close();
            }
        }
    }
}


主程序调用

MainWindow.xaml.cs

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 WpfApplicationProgressBarDemo
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        WindowProgressBar windowProgressBar = null;

        public MainWindow()
        {
            InitializeComponent();

            RunProgressBar();
        }

        private void RunProgressBar()
        {
            windowProgressBar = new WindowProgressBar(10);
            windowProgressBar.WindowStartupLocation = WindowStartupLocation.CenterScreen;
            windowProgressBar.Show();
            windowProgressBar.Activate();

            for (int i = 0; i < 10; i++)
            {
                Thread.Sleep(1000);
                windowProgressBar.SetProgressBarValue(i + 1);
            }
        }
    }
}


运行效果


猜你喜欢

转载自blog.csdn.net/empty_android/article/details/79392986
今日推荐