开发者论坛

 找回密码
 注册 (请使用非IE浏览器)
查看: 5351|回复: 1

XAF之弹出式进度条 (转)

[复制链接]

0

精华

161

贡献

57

赞扬

帖子
112
软币
1615
在线时间
263 小时
注册时间
2013-6-14
发表于 2013-6-19 17:01:34 | 显示全部楼层 |阅读模式
本帖最后由 linuxsc 于 2013-6-19 17:03 编辑

(转自:http://blog.csdn.net/qjpcpu/article/details/7404746)
一直想加一个进度条到XAF的工程中去,最后发现两条途径:
1.自定义模板,加入进度条;
2.动态弹出一个进度条;
自定义模板就不说了,可以参照官方文档的例子做,下面说说弹出进度条,如图:


        这个例子是我模仿FeatureCenter部分代码做的,下面两个文件(LongOperationController.cs和ProgressForm.cs)的代码无须更改,可直接使用。
LongOperationController.cs的源码:
  1. <p>using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using DevExpress.ExpressApp;
  6. using System.ComponentModel;</p><p>namespace FunSolution.Module
  7. {
  8.     public interface IProgressControl : IDisposable
  9.     {
  10.         void ShowProgress(BackgroundWorker worker);
  11.     }
  12.     public abstract class LongOperationController : ViewController
  13.     {
  14.         private IProgressControl progressControl;
  15.         private BackgroundWorker _worker;
  16.         public BackgroundWorker Worker
  17.         {
  18.             private set { _worker = value; }
  19.             get { return _worker; }
  20.         }
  21.         private void Work_Completed(object sender,RunWorkerCompletedEventArgs e)
  22.         {
  23.             OnOperationCompleted();
  24.             progressControl.Dispose();
  25.             progressControl = null;
  26.             ((BackgroundWorker)sender).RunWorkerCompleted -= new RunWorkerCompletedEventHandler(Work_Completed);
  27.             ((BackgroundWorker)sender).Dispose();
  28.         }
  29.         protected abstract void DoWorkCore(object sender, DoWorkEventArgs e);
  30.         protected abstract IProgressControl CreateProgressControl();</p><p>        protected virtual void OnOperationCompleted()
  31.         {
  32.             View.ObjectSpace.Refresh();
  33.             if (OperationCompleted != null)
  34.             {
  35.                 OperationCompleted(this, EventArgs.Empty);
  36.             }
  37.         }
  38.         protected void StartLongOperation()
  39.         {
  40.             Worker = new BackgroundWorker();
  41.             Worker.WorkerReportsProgress = true;
  42.             Worker.WorkerSupportsCancellation = true;
  43.             Worker.RunWorkerCompleted += new RunWorkerCompletedEventHandler(Work_Completed);
  44.             Worker.DoWork += new DoWorkEventHandler(DoWorkCore);
  45.             progressControl = CreateProgressControl();
  46.             progressControl.ShowProgress(Worker);
  47.             Worker.RunWorkerAsync();
  48.         }
  49.         protected void RaiseProcessChanged(int percentage)
  50.         {
  51.             Worker.ReportProgress(percentage);
  52.         }
  53.         protected bool CancellationPending
  54.         {
  55.             get { return Worker.CancellationPending; }
  56.         }</p><p>        //子类可订阅也可不订阅
  57.         public event EventHandler OperationCompleted;
  58.     }
  59. }
  60. </p></p>
复制代码
下面是ProgressForm.cs的源码,负责显示部分:
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text;
  4. using DevExpress.XtraEditors;
  5. using System.Windows.Forms;
  6. using DevExpress.ExpressApp;
  7. using System.ComponentModel;
  8. using System.Drawing;
  9. using FunSolution.Module;

  10. namespace FunSolution.Module.Win
  11. {
  12.     public class ProgressForm : IProgressControl {
  13.         public const string FormName = "ProgressForm";
  14.         private XtraForm form;
  15.         private ProgressBarControl progressBar = new ProgressBarControl();
  16.         private SimpleButton cancelButton = new SimpleButton();
  17.         private Label label = new Label();
  18.         private BackgroundWorker worker;
  19.         private int minimumProgressValue;
  20.         private int maximumProgressValue;
  21.         private string progressFormCaption;
  22.         private delegate void UpdateProgressFormDelegate(int value, string message);

  23.         private void CreateProgressForm() {
  24.             form = new XtraForm();
  25.             form.Name = FormName;
  26.             form.Width = 350;
  27.             form.Height = 125;
  28.             form.StartPosition = FormStartPosition.CenterScreen;
  29.             form.FormBorderStyle = FormBorderStyle.FixedDialog;
  30.             form.MinimizeBox = false;
  31.             form.MaximizeBox = false;
  32.             form.ControlBox = false;
  33.             form.ShowInTaskbar = false;

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

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

  44.             cancelButton.Parent = form;
  45.             cancelButton.DialogResult = DialogResult.Cancel;
  46.             cancelButton.Size = new Size(75, 23);
  47.             cancelButton.Location = new Point((form.Width - cancelButton.Width) / 2, 55);
  48.             cancelButton.Text = "&Cancel";
  49.             cancelButton.Click += new EventHandler(cancelButton_Click);
  50.             cancelButton.LostFocus += new EventHandler(DoOnFormLostFocus);
  51.             form.CancelButton = cancelButton;
  52.             form.Text = progressFormCaption;
  53.         }
  54.         private void DoOnFormLostFocus(object sender, EventArgs e) {
  55.             form.Focus();
  56.         }
  57.         private void LongOperation_Completed(object sender, RunWorkerCompletedEventArgs  e) {
  58.             if(form != null) {
  59.                 form.Invoke(new MethodInvoker(form.Close));
  60.             }
  61.         }
  62.         private void UpdateProgressForm(int value, string message) {
  63.             progressBar.EditValue = value;
  64.             progressBar.Update();
  65.             label.Text = message;
  66.         }
  67.         private void LongOperation_ProgressChanged(object sender, ProgressChangedEventArgs e)
  68.         {
  69.             int value = e.ProgressPercentage;
  70.             if(form != null) {
  71.                 form.Invoke(new UpdateProgressFormDelegate(UpdateProgressForm), new object[] { value, null });
  72.             }
  73.         }
  74.         private void cancelButton_Click(object sender, EventArgs e) {
  75.             if (worker.WorkerSupportsCancellation == true)
  76.             {
  77.                 worker.CancelAsync();
  78.             }
  79.         }
  80.         
  81.         protected ProgressForm(int minimum, int maximum)
  82.             : this("", minimum, maximum) {
  83.         }
  84.         public ProgressForm(string caption, int minimum, int maximum) {
  85.             this.progressFormCaption = caption;
  86.             this.minimumProgressValue = minimum;
  87.             this.maximumProgressValue = maximum;
  88.             CreateProgressForm();
  89.         }
  90.         public ProgressForm()
  91.             : this(0, 100) {
  92.         }
  93.         public void Dispose() {
  94.             label = null;
  95.             cancelButton.LostFocus -= new EventHandler(DoOnFormLostFocus);
  96.             this.worker.ProgressChanged -= new ProgressChangedEventHandler(LongOperation_ProgressChanged);
  97.             this.worker.RunWorkerCompleted -= new RunWorkerCompletedEventHandler(LongOperation_Completed);
  98.             
  99.             if(form != null) {
  100.                 form.Invoke(new MethodInvoker(form.Dispose));
  101.                 form = null;
  102.             }
  103.         }
  104.         public void ShowProgress(BackgroundWorker worker) {
  105.             form.Show();
  106.             this.worker = worker;
  107.             this.worker.ProgressChanged += new ProgressChangedEventHandler(LongOperation_ProgressChanged);
  108.             this.worker.RunWorkerCompleted += new RunWorkerCompletedEventHandler(LongOperation_Completed);
  109.         }
  110.     }
  111. }
复制代码
接下来就是自己新加的ViewController,添加一个ViewController使它继承LongOperationController,并重写以下方法:
  1. protected override void DoWorkCore(object sender, DoWorkEventArgs e)

  2. protected  override IProgressControl CreateProgressControl()
复制代码
DoWorkCore是要进行的耗时的具体操作,CreateProgressControl创建进度条窗体。

另外,添加一个Action调用继承而来的StartLongOperation。
  1. public partial class ViewController1 : LongOperationController
  2.     {
  3.         public ViewController1()
  4.         {
  5.             InitializeComponent();
  6.             RegisterActions(components);
  7.         }
  8.         protected override void DoWorkCore(object sender, DoWorkEventArgs e)
  9.         {
  10.             int i=0;
  11.             while(i++<100){
  12.                 if (CancellationPending == true)
  13.                     break;
  14.                 Thread.Sleep(50);
  15.                 this.RaiseProcessChanged(i);
  16.             }
  17.         }
  18.         protected  override IProgressControl CreateProgressControl()
  19.         {
  20.             return new ProgressForm("操作进行中", 0, 100);
  21.         }

  22.         private void simpleAction1_Execute(object sender, SimpleActionExecuteEventArgs e)
  23.         {
  24.             this.StartLongOperation();
  25.         }
  26.     }
复制代码

评分

参与人数 2贡献 +4 赞扬 +1 收起 理由
huashan + 1 赞一个
羽叶 + 4 赞一个

查看全部评分

回复

使用道具 举报

0

精华

2

贡献

0

赞扬

帖子
4
软币
94
在线时间
2 小时
注册时间
2013-7-12
发表于 2013-7-12 17:31:28 | 显示全部楼层
这个功能好象现在的版本中提供了更好的控件了,不过还是赞一个
回复

使用道具 举报

Archiver|手机版|小黑屋|开发者网 ( 苏ICP备08004430号-2 )
版权所有:南京韵文教育信息咨询有限公司

GMT+8, 2024-6-20 03:39

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表