WPF ProgressDialog

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

ProgressDialog.xaml



    
        
            
            
            
        
        
        
        
            
                
                
            
            
            

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
{
    /// 
    /// ProgressDialog.xaml 的交互逻辑
    /// 
    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();
            }

WPF ProgressDialog_第1张图片

当然,也能用BusyIndicator 代替ProgressDialog 。

 

转载于:https://www.cnblogs.com/Ivan83/archive/2011/10/14/2212530.html

你可能感兴趣的:(WPF ProgressDialog)