本文内容来自Microsoft Hands-on Labs for Windows Workflow Foundation中的HOL02中的第四个练习,其中包括两个任务:
添加一个验证来检查Email参数。
Ø 添加Activity Designer
Ø 测试Activity Designer
在这个练习中,将增强SendMailActivity的设计时界面。
Activity Designer定义了Workflow中activity的直观表示和自定义活动的设计。
1.在解决方案资源管理器中,在SendMailActivityLibrary中添加一个Code File,名字为SendMailDesigner.cs。
2.在SendMailDesigner.cs中添加如下代码,其中SendMailDesigner要继承自ActivityDesigner。
using System;
using System.Text;
using System.ComponentModel;
using System.ComponentModel.Design;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Drawing.Design;
using System.Workflow.ComponentModel.Compiler;
using System.Workflow.ComponentModel.Serialization;
using System.Workflow.ComponentModel;
using System.Workflow.ComponentModel.Design;
using System.Workflow.Runtime;
using System.Workflow.Activities;
using System.Workflow.Activities.Rules;
namespace SendMailActivityLibrary
{
public class SendMailDesigner : ActivityDesigner
{
}
}
3.我们需要重载3个方法,Initalize、OnPaint和OnLayoutSize,在下面的步骤中将详细描述需要在哪些地方做什么。
4.Initalize,我们将重载这个方法来保存将演示自定义设计Activity引用。我们也需要基类同样初始化。在SendMailDesigner类中输入如下代码:
SendMailActivity parentActivity;
protected override void Initialize(Activity activity)
{
base.Initialize(activity);
parentActivity = (SendMailActivity)activity;
}
5.OnLayoutSize方法,覆写这个方法为了给自定义绘出对象提供关于大小的信息。在SendMailDesigner类中输入以下代码:
protected override Size OnLayoutSize(ActivityDesignerLayoutEventArgs e)
{
return new Size(230, 100);
}
6.OnPaint方法,覆写这个方法为了展现自定义绘图。在SendMailDesigner类中输入以下代码:
Rectangle frameRect = new Rectangle(this.Location.X, this.Location.Y, this.Size.Width - 5, this.Size.Height - 5);
Rectangle shadowRect = new Rectangle(frameRect.X + 5, frameRect.Y + 5, frameRect.Width, frameRect.Height);
Rectangle pageRect = new Rectangle(frameRect.X + 4, frameRect.Y + 24, frameRect.Width - 8, frameRect.Height - 28);
Rectangle titleRect = new Rectangle(frameRect.X + 15, frameRect.Y + 4, frameRect.Width / 2, 20);
Brush frameBrush = new LinearGradientBrush(frameRect, Color.DarkBlue, Color.LightBlue, 45);
e.Graphics.FillPath(Brushes.LightGray, RoundedRect(shadowRect));
e.Graphics.FillPath(frameBrush, RoundedRect(frameRect));
e.Graphics.FillPath(new LinearGradientBrush(pageRect, Color.White, Color.WhiteSmoke, 45), RoundedRect(pageRect));
e.Graphics.DrawString(Activity.QualifiedName, new Font("Segoe UI", 9), Brushes.White, titleRect);
frameRect.Inflate(20, 20);
string textToDisplay = String.Format("To : \'{0}\'\r\nFrom : \'{1}\'\r\nSubject : \'{2}\'\r\n", parentActivity.To, parentActivity.From, parentActivity.Subject);
e.Graphics.DrawString(String.Format(textToDisplay, parentActivity.Subject), new Font("Segoe UI", 8), Brushes.Black, pageRect.X, pageRect.Y + 15);
7.注意到其中调用了RoundedRect公用方法,用来生成圆角显示。下面来编写这个方法。
private GraphicsPath RoundedRect(Rectangle frame)
{
GraphicsPath path = new GraphicsPath();
int radius = 7;
int diameter = radius * 2;
Rectangle arc = new Rectangle(frame.Left, frame.Top, diameter, diameter);
path.AddArc(arc, 180, 90);
arc.X = frame.Right - diameter;
path.AddArc(arc, 270, 90);
arc.Y = frame.Bottom - diameter;
path.AddArc(arc, 0, 90);
arc.X = frame.Left;
path.AddArc(arc, 90, 90);
path.CloseFigure();
return path;
}
8.给SendMailActivity类增加如下Designer属性
[Designer(typeof(SendMailDesigner), typeof(IDesigner))]
9.生成解决方案。
任务一完成。
10.切换到SendMailWorkflow设计视图。SendMailWorkflow的样式变成如下样式:
11.在From属性中输入一个错误的Email格式地址,将会出现红色圆圈叹号的提示。
12.将To的Email地址格式修改正确,测试。
13.保存所有文件,并编译退出。
任务二完成。
参考资料: