WPF ProgressDialog

得闲无事,做了个ProgressDialog。希望对大家有用。

ProgressDialog.xaml

<Window x:Class="WpfApplication1.ProgressDialog"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="ProgressDialog"  Width="310" Height="100" 
        Topmost="True" ShowInTaskbar="False" WindowStartupLocation="CenterScreen"
        WindowStyle="None" ResizeMode="NoResize" BorderBrush="Silver" BorderThickness="1">

    <Grid Margin="5" Background="Honeydew">
        <Grid.RowDefinitions>
            <RowDefinition Height="25"></RowDefinition>
            <RowDefinition Height="30"></RowDefinition>
            <RowDefinition Height="35"></RowDefinition>
        </Grid.RowDefinitions>
        <TextBlock Text="处理中。。。"  Name="label1"  Grid.Row="0" VerticalAlignment="Center" x:FieldModifier="private" />
        <ProgressBar Name="progressBar1" Grid.Row="1" Maximum="100" x:FieldModifier="private" />
        <Grid Grid.Row="2" Margin="0,5" >
            <Grid.ColumnDefinitions>
                <ColumnDefinition></ColumnDefinition>
                <ColumnDefinition Width="60"></ColumnDefinition>
            </Grid.ColumnDefinitions>
            <TextBlock  Name="infoBlock"  VerticalAlignment="Center" x:FieldModifier="private" />
            <Button Grid.Column="1" Content="取消" Width="50" Click="Button_Click" HorizontalAlignment="Right" />
        </Grid>
    </Grid>
</Window>

ProgressDialog.xaml.cs

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

namespace WpfApplication1
{
    /// <summary>
    /// ProgressDialog.xaml 的交互逻辑
    /// </summary>
    public partial class ProgressDialog : Window
    {
        public ProgressDialog()
        {
            InitializeComponent();
        }
        public void SetProgressValue(double value)
        {
            this.progressBar1.Value = value;
        }

        public void SetMessage(string mess)
        {
            this.infoBlock.Text = mess;
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            throw new UserExitException();
        }
    }

    public class UserExitException : Exception
    {
        public UserExitException()
            : base("用户退出!!")
        {
        }
    }
}

ProgressDialogHelper.cs(辅助类)

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.ComponentModel;
using System.Threading;

namespace WpfApplication1
{
    public class ProgressDialogHelper
    {
        public ProgressDialog ProDialog ;
        public Thread Worker;

        public void CloseProgressDialog()
        {
            ProDialog.Dispatcher.BeginInvoke(new Action(() =>
            {
                ProDialog.Close();
            }));
        }

        public void WorkerThreadAbort()
        {
            if (Worker.IsAlive)
                Worker.Abort();
        }

        public void SetValueAndMessage(double value, string mess)
        {
            ProDialog.Dispatcher.BeginInvoke(new Action(() =>
            {
                ProDialog.SetProgressValue(value);
                ProDialog.SetMessage(mess);
            }));
        }

        public void SetValue(double value)
        {
            ProDialog.Dispatcher.BeginInvoke(new Action(() =>
            {
                ProDialog.SetProgressValue(value);
            }));
        }

        public void SetMessage(string mess)
        {
            ProDialog.Dispatcher.BeginInvoke(new Action(() =>
            {
                ProDialog.SetMessage(mess);
            }));
        }

        public void Show(Action workAction)
        {
            this.Worker = new Thread(new ThreadStart(workAction));
            this.ProDialog = new ProgressDialog();

            Worker.Start();
            ProDialog.ShowDialog();
        }
    }
}

调用方法

            ProgressDialogHelper pdh = null;
            try
            {
                pdh = new ProgressDialogHelper();
                pdh.Show(() =>
                {  //耗时操作
                    for (int i = 0; i < 100; i++)
                    {
                        System.Threading.Thread.Sleep(100);
                        pdh.SetValue(i);//控制进度。
                    }
                    //耗时操作内关闭ProgressDialog
                    pdh.CloseProgressDialog();
                });
            }
            catch
            {  //异常时退出
                pdh.CloseProgressDialog();
                pdh.WorkerThreadAbort();
            }

当然,也能用BusyIndicator 代替ProgressDialog 。

------------------------------------------------------------
如非注明都是原创,如需转载请注出处。
原文地址:https://www.cnblogs.com/Ivan83/p/2212530.html