XAF之弹出式进度条

一直想加一个进度条到XAF的工程中去,最后发现两条途径:

1.自定义模板,加入进度条;

2.动态弹出一个进度条;

自定义模板就不说了,可以参照官方文档的例子做,下面说说弹出进度条,如图:


        这个例子是我模仿FeatureCenter部分代码做的,下面两个文件(LongOperationController.cs和ProgressForm.cs)的代码无须更改,可直接使用。

LongOperationController.cs的源码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DevExpress.ExpressApp;
using System.ComponentModel;

namespace FunSolution.Module
{
    public interface IProgressControl : IDisposable
    {
        void ShowProgress(BackgroundWorker worker);
    }
    public abstract class LongOperationController : ViewController
    {
        private IProgressControl progressControl;
        private BackgroundWorker _worker;
        public BackgroundWorker Worker
        {
            private set { _worker = value; }
            get { return _worker; }
        }
        private void Work_Completed(object sender,RunWorkerCompletedEventArgs e)
        {
            OnOperationCompleted();
            progressControl.Dispose();
            progressControl = null;
            ((BackgroundWorker)sender).RunWorkerCompleted -= new RunWorkerCompletedEventHandler(Work_Completed);
            ((BackgroundWorker)sender).Dispose();
        }
        protected abstract void DoWorkCore(object sender, DoWorkEventArgs e);
        protected abstract IProgressControl CreateProgressControl();

        protected virtual void OnOperationCompleted()
        {
            View.ObjectSpace.Refresh();
            if (OperationCompleted != null)
            {
                OperationCompleted(this, EventArgs.Empty);
            }
        }
        protected void StartLongOperation()
        {
            Worker = new BackgroundWorker();
            Worker.WorkerReportsProgress = true;
            Worker.WorkerSupportsCancellation = true;
            Worker.RunWorkerCompleted += new RunWorkerCompletedEventHandler(Work_Completed);
            Worker.DoWork += new DoWorkEventHandler(DoWorkCore);
            progressControl = CreateProgressControl();
            progressControl.ShowProgress(Worker);
            Worker.RunWorkerAsync();
        }
        protected void RaiseProcessChanged(int percentage)
        {
            Worker.ReportProgress(percentage);
        }
        protected bool CancellationPending
        {
            get { return Worker.CancellationPending; }
        }

        //子类可订阅也可不订阅
        public event EventHandler OperationCompleted;
    }
}
下面是ProgressForm.cs的源码,负责显示部分:

using System;
using System.Collections.Generic;
using System.Text;
using DevExpress.XtraEditors;
using System.Windows.Forms;
using DevExpress.ExpressApp;
using System.ComponentModel;
using System.Drawing;
using FunSolution.Module;

namespace FunSolution.Module.Win
{
    public class ProgressForm : IProgressControl {
        public const string FormName = "ProgressForm";
        private XtraForm form;
        private ProgressBarControl progressBar = new ProgressBarControl();
        private SimpleButton cancelButton = new SimpleButton();
        private Label label = new Label();
        private BackgroundWorker worker;
        private int minimumProgressValue;
        private int maximumProgressValue;
        private string progressFormCaption;
        private delegate void UpdateProgressFormDelegate(int value, string message);

        private void CreateProgressForm() {
            form = new XtraForm();
            form.Name = FormName;
            form.Width = 350;
            form.Height = 125;
            form.StartPosition = FormStartPosition.CenterScreen;
            form.FormBorderStyle = FormBorderStyle.FixedDialog;
            form.MinimizeBox = false;
            form.MaximizeBox = false;
            form.ControlBox = false;
            form.ShowInTaskbar = false;

            label.Parent = form;
            label.Location = new Point(10, 10);
            label.Size = new Size(form.ClientSize.Width - 20, 13);

            progressBar.Parent = form;
            progressBar.Location = new Point(10, 30);
            progressBar.Name = "progressBar";
            progressBar.Size = new Size(form.ClientSize.Width - 20, 15);
            progressBar.Properties.Minimum = minimumProgressValue;
            progressBar.Properties.Maximum = maximumProgressValue;
            progressBar.Properties.Step = 1;

            cancelButton.Parent = form;
            cancelButton.DialogResult = DialogResult.Cancel;
            cancelButton.Size = new Size(75, 23);
            cancelButton.Location = new Point((form.Width - cancelButton.Width) / 2, 55);
            cancelButton.Text = "&Cancel";
            cancelButton.Click += new EventHandler(cancelButton_Click);
            cancelButton.LostFocus += new EventHandler(DoOnFormLostFocus);
            form.CancelButton = cancelButton;
            form.Text = progressFormCaption;
        }
        private void DoOnFormLostFocus(object sender, EventArgs e) {
            form.Focus();
        }
        private void LongOperation_Completed(object sender, RunWorkerCompletedEventArgs  e) {
            if(form != null) {
                form.Invoke(new MethodInvoker(form.Close));
            }
        }
        private void UpdateProgressForm(int value, string message) {
            progressBar.EditValue = value;
            progressBar.Update();
            label.Text = message;
        }
        private void LongOperation_ProgressChanged(object sender, ProgressChangedEventArgs e)
        {
            int value = e.ProgressPercentage;
            if(form != null) {
                form.Invoke(new UpdateProgressFormDelegate(UpdateProgressForm), new object[] { value, null });
            }
        }
        private void cancelButton_Click(object sender, EventArgs e) {
            if (worker.WorkerSupportsCancellation == true)
            {
                worker.CancelAsync();
            }
        }
        
        protected ProgressForm(int minimum, int maximum)
            : this("", minimum, maximum) {
        }
        public ProgressForm(string caption, int minimum, int maximum) {
            this.progressFormCaption = caption;
            this.minimumProgressValue = minimum;
            this.maximumProgressValue = maximum;
            CreateProgressForm();
        }
        public ProgressForm()
            : this(0, 100) {
        }
        public void Dispose() {
            label = null;
            cancelButton.LostFocus -= new EventHandler(DoOnFormLostFocus);
            this.worker.ProgressChanged -= new ProgressChangedEventHandler(LongOperation_ProgressChanged);
            this.worker.RunWorkerCompleted -= new RunWorkerCompletedEventHandler(LongOperation_Completed);
            
            if(form != null) {
                form.Invoke(new MethodInvoker(form.Dispose));
                form = null;
            }
        }
        public void ShowProgress(BackgroundWorker worker) {
            form.Show();
            this.worker = worker;
            this.worker.ProgressChanged += new ProgressChangedEventHandler(LongOperation_ProgressChanged);
            this.worker.RunWorkerCompleted += new RunWorkerCompletedEventHandler(LongOperation_Completed);
        }
    }
}

接下来就是自己新加的ViewController,添加一个ViewController使它继承LongOperationController,并重写以下方法:

protected override void DoWorkCore(object sender, DoWorkEventArgs e)
protected  override IProgressControl CreateProgressControl()
DoWorkCore是要进行的耗时的具体操作,CreateProgressControl创建进度条窗体。

另外,添加一个Action调用继承而来的StartLongOperation。

public partial class ViewController1 : LongOperationController
    {
        public ViewController1()
        {
            InitializeComponent();
            RegisterActions(components);
        }
        protected override void DoWorkCore(object sender, DoWorkEventArgs e)
        {
            int i=0;
            while(i++<100){
                if (CancellationPending == true)
                    break;
                Thread.Sleep(50);
                this.RaiseProcessChanged(i);
            }
        }
        protected  override IProgressControl CreateProgressControl()
        {
            return new ProgressForm("操作进行中", 0, 100);
        }

        private void simpleAction1_Execute(object sender, SimpleActionExecuteEventArgs e)
        {
            this.StartLongOperation();
        }
    }





  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值