尽管WF提供了很多的活动,但是还是有些情况需要开发人员自己开发工作流活动。例如:我们要执行一条SQL语句。
接下来我们开发一个简单的活动。
它有一下特征:
1.自定义工具箱图标
2.自定义工具箱中活动的名称
3.自定义设置器
4.自定义主题
5.自定义验证
6.自定义执行的代码
首先,新建一个工作流活动库,如图:
接下来,新建一个活动,如图:
在新添加的活动中找到"Base类"这个属性。设置这个属性,让其继承自System.Workflow.ComponentModel.Activity,如图:
接下来我们来编写活动的代码,代码如下:
Code
1 public GreetActivity()
2 {
3 InitializeComponent();
4 }
5
6 public static DependencyProperty 用户名Property = DependencyProperty.Register("用户名", typeof(string), typeof(GreetActivity));
7 public static DependencyProperty 问候语Property = DependencyProperty.Register("问候语", typeof(string), typeof(GreetActivity));
8
9
10 public string 用户名
11 {
12 get { return (string)base.GetValue(用户名Property); }
13 set { base.SetValue(用户名Property, value); }
14 }
15
16 public string 问候语
17 {
18 get { return (string)base.GetValue(问候语Property); }
19 set { base.SetValue(问候语Property, value); }
20 }
21
22 protected override ActivityExecutionStatus Execute(ActivityExecutionContext executionContext)
23 {
24 Console.WriteLine(用户名 + ":" + 问候语);
25 return base.Execute(executionContext);
26 }
这段代码声明了两个属性,最后重写了基类的Excute方法向控制台输出了这两个属性的信息。
这个简单活动我们就做完了。
执行代码结果如图:
接下来我们对这个活动进行下扩展,让它变的更美观。
首先新增一个MyActivityToolboxItem类,代码如下:
Code
1 using System;
2 using System.Collections.Generic;
3 using System.Linq;
4 using System.Text;
5 using System.Drawing;
6 using System.Workflow.ComponentModel.Design;
7 using System.Runtime.Serialization;
8 namespace MyActivityLibrary
9 {
10 [Serializable]
11 public class MyActivityToolboxItem : ActivityToolboxItem
12 {
13 public MyActivityToolboxItem()
14 {
15 }
16
17 public MyActivityToolboxItem(Type type)
18 : base(type)
19 {
20 base.DisplayName = type.Name.Substring(0, type.Name.Length - 8);
21 }
22
23 protected MyActivityToolboxItem(SerializationInfo info, StreamingContext context)
24 {
25 Deserialize(info, context);
26 }
27 }
28 }
这段代码改变了工具箱中的活动的默认名称,将活动的名称中的Activity去掉。
在增加一个GreetActivityDesigner类,代码如下:
Code
1 [ActivityDesignerTheme(typeof(GreetDesignerTheme))]
2 public class GreetActivityDesigner : ActivityDesigner
3 {
4 private const int TEXT_WIDTH = 75;
5 private const int PADDING = 4;
6
7 protected override Rectangle ImageRectangle
8 {
9 get
10 {
11 Rectangle rectangle = new Rectangle();
12 rectangle.X = this.Bounds.Left + PADDING;
13 rectangle.Y = this.Bounds.Top + PADDING;
14 rectangle.Size = Properties.Resources.问候.Size;
15 return rectangle;
16 }
17 }
18
19 protected override Rectangle TextRectangle
20 {
21 get
22 {
23 Rectangle imgRect = this.ImageRectangle;
24
25 Rectangle rectangle = new Rectangle(
26 imgRect.Right + PADDING,
27 imgRect.Top,
28 TEXT_WIDTH,
29 imgRect.Height);
30 return rectangle;
31 }
32 }
33
34 protected override void Initialize(System.Workflow.ComponentModel.Activity activity)
35 {
36 base.Initialize(activity);
37
38 Bitmap bitmap = Properties.Resources.问候;
39 this.Image = bitmap;
40
41 }
42
43 protected override Size OnLayoutSize(ActivityDesignerLayoutEventArgs e)
44 {
45 base.OnLayoutSize(e);
46
47 Size imgSize = Properties.Resources.问候.Size;
48 return new Size(imgSize.Width + TEXT_WIDTH + (PADDING * 3),
49 imgSize.Height + (PADDING * 2));
50 }
51 }
这个类定义了活动中的图片和文字是如何显示的。
新增GreetDesignerTheme类,代码如下:
Code
1 public class GreetDesignerTheme : ActivityDesignerTheme
2 {
3 public GreetDesignerTheme(WorkflowTheme theme)
4 : base(theme)
5 {
6 }
7
8 public override void Initialize()
9 {
10 this.ForeColor = Color.Black;
11 this.BorderColor = Color.Black;
12 this.BorderStyle = DashStyle.Solid;
13 this.BackgroundStyle = LinearGradientMode.ForwardDiagonal;
14 this.BackColorStart = Color.Black;
15 this.BackColorEnd = Color.Black;
16 this.ForeColor = Color.White;
17 base.Initialize();
18 }
19 }
这个类改变了活动的颜色以及文字的颜色等。
增加GreetActivityValidator.cs类,代码如下:
Code
1 public class GreetActivityValidator:Validator
2 {
3 public override ValidationErrorCollection Validate(ValidationManager manager, object obj)
4 {
5 GreetActivity greetActivity = obj as GreetActivity;
6 ValidationErrorCollection errorCollection = base.Validate(manager, obj);
7 if (greetActivity.Parent!=null)
8 {
9 if (string.IsNullOrEmpty(greetActivity.问候语))
10 {
11 errorCollection.Add(ValidationError.GetNotSetValidationError("问候语"));
12 }
13 if (string.IsNullOrEmpty(greetActivity.用户名))
14 {
15 errorCollection.Add(ValidationError.GetNotSetValidationError("用户名"));
16 }
17 }
18 return errorCollection;
19 }
20 }
这个类用来检查活动中的属性是否被设置,如果没有设置就会出现红色感叹号提示开发人员。
最后,我们配置一下刚才开发的那个活动,添加如下代码:
Code
1 [ActivityValidator(typeof(GreetActivityValidator))]
2 [ToolboxBitmap(typeof(GreetActivity), "Resources.问候.ico")]
3 [ToolboxItem(typeof(MyActivityToolboxItem))]
4 [Designer(typeof(GreetActivityDesigner), typeof(IDesigner))]
让我们来看下美化前和美化后的活动。
怎么样是不是好看多了。
浙公网安备 33010602011771号