通过AEC解读WF的核心原理(七)一个实现Goto功能的Activity

说明

例子下载:https://files.cnblogs.com/wxwinter/aec7.rar

 WF的工具栏里竞然没的Goto,SetState不算错

 Goto好不好用是开发人员的问题,不提供就是平台的错了。

实现Goto不难,难的是限定Goto的规则,本例的Goto规则是可以跳到同级容器内的任意Activity处开始持行

本例是下个ACE和调度技巧演示,没的具体的业务场景

 本例还演示控制Activity面板显示的技巧:Designer

 例子

Goto的Designer

public class myDesigner : ActivityDesigner

{

Goto parentActivity;

protected override void Initialize(Activity activity)

{

base.Initialize(activity);

parentActivity = (Goto)activity;

}

//定义Activity的尺寸

protected override Size OnLayoutSize(ActivityDesignerLayoutEventArgs e)

{

return new Size(100, 80);

}

//Activity的面板展现

protected override void OnPaint(ActivityDesignerPaintEventArgs e)

{

Rectangle 区域 = new Rectangle(this.Location.X, this.Location.Y, this.Size.Width, this.Size.Height);

GraphicsPath path = new GraphicsPath();

path.AddRectangle(区域);

Brush 笔刷 = new LinearGradientBrush(区域, Color.White, Color.LightGreen, 1);

e.Graphics.FillPath(笔刷, path);

e.Graphics.DrawRectangle(new Pen(Brushes.Black), 区域);

string s = Activity.QualifiedName + "\r\n" + "\r\n";

s = s + "跳到:" + "\r\n";

s = s + parentActivity.要跳到的位置.Name + "\r\n";

e.Graphics.DrawString(s, new Font("宋体", 12), Brushes.Black, 区域);

}

}

Goto的Activity

[Designer(typeof(myDesigner), typeof(IDesigner))]

    public class Goto: Activity

    {

public Activity 要跳到的位置

{

get { return (Activity )GetValue(要跳到的位置Property); }

set { SetValue(要跳到的位置Property, value); }

}

public static readonly DependencyProperty 要跳到的位置Property = DependencyProperty.Register("要跳到的位置", typeof(Activity ), typeof(Goto));

public int 记数器

{

get { return (int)GetValue(记数器Property); }

set { SetValue(记数器Property, value); }

}

public static readonly DependencyProperty 记数器Property =DependencyProperty.Register("记数器", typeof(int), typeof(Goto),new PropertyMetadata(1));

    }

容器Activity

public class 容器: SequenceActivity

    {

int index=0;

protected override ActivityExecutionStatus Execute(ActivityExecutionContext executionContext)

{

ExecuteActivityItem(executionContext);

return ActivityExecutionStatus.Executing;

}

private void ExecuteActivityItem(ActivityExecutionContext executionContext)

{

Activity activity = EnabledActivities[index];

if (activity is Goto)

{

Goto p = this.GetActivityByName(activity.Name) as Goto;

if (p.记数器 == 0)

{

index = index + 1;

ExecuteActivityItem(executionContext);

}

else

{

p.记数器 = p.记数器 - 1;

Goto tp = activity as Goto;

index = this.EnabledActivities.IndexOf(tp.要跳到的位置);

ExecuteActivityItem(executionContext);

}

}

else

{

ActivityExecutionContext newContext = executionContext.ExecutionContextManager.CreateExecutionContext(activity);

newContext.Activity.Closed += activity_Closed;

newContext.ExecuteActivity(newContext.Activity);

}

}

void activity_Closed(object sender, ActivityExecutionStatusChangedEventArgs e)

{

e.Activity.Closed -= activity_Closed;

index = index + 1;

ActivityExecutionContext aec = sender as ActivityExecutionContext;

ActivityExecutionContext childContext = aec.ExecutionContextManager.GetExecutionContext(e.Activity);

aec.ExecutionContextManager.CompleteExecutionContext(childContext);

if (index > this.EnabledActivities.Count - 1)

{

aec.CloseActivity();

}

else

{

ExecuteActivityItem(aec);

}

}

}

测试用工作流

    public class Workflow1: SequentialWorkflowActivity

{

private CodeActivity codeActivity4;

private CodeActivity codeActivity3;

private CodeActivity codeActivity2;

private 容器 容器1;

private Goto goto1;

private CodeActivity codeActivity5;

private CodeActivity codeActivity1;

        public Workflow1()

        {

            InitializeComponent();

        }

[System.Diagnostics.DebuggerNonUserCode]

private void InitializeComponent()

{

this.CanModifyActivities = true;

this.codeActivity5 = new System.Workflow.Activities.CodeActivity();

this.goto1 = new wxwinterAecTest.Goto();

this.codeActivity4 = new System.Workflow.Activities.CodeActivity();

this.codeActivity3 = new System.Workflow.Activities.CodeActivity();

this.codeActivity2 = new System.Workflow.Activities.CodeActivity();

this.codeActivity1 = new System.Workflow.Activities.CodeActivity();

this.容器1 = new wxwinterAecTest.容器();

//

// codeActivity5

//

this.codeActivity5.Name = "codeActivity5";

this.codeActivity5.ExecuteCode += new System.EventHandler(this.codeActivity_ExecuteCode);

//

// goto1

//

this.goto1.Name = "goto1";

this.goto1.要跳到的位置 = this.codeActivity2;

this.goto1.记数器 = 1;

//

// codeActivity4

//

this.codeActivity4.Name = "codeActivity4";

this.codeActivity4.ExecuteCode += new System.EventHandler(this.codeActivity_ExecuteCode);

//

// codeActivity3

//

this.codeActivity3.Name = "codeActivity3";

this.codeActivity3.ExecuteCode += new System.EventHandler(this.codeActivity_ExecuteCode);

//

// codeActivity2

//

this.codeActivity2.Name = "codeActivity2";

this.codeActivity2.ExecuteCode += new System.EventHandler(this.codeActivity_ExecuteCode);

//

// codeActivity1

//

this.codeActivity1.Name = "codeActivity1";

this.codeActivity1.ExecuteCode += new System.EventHandler(this.codeActivity_ExecuteCode);

//

// 容器1

//

this.容器1.Activities.Add(this.codeActivity1);

this.容器1.Activities.Add(this.codeActivity2);

this.容器1.Activities.Add(this.codeActivity3);

this.容器1.Activities.Add(this.codeActivity4);

this.容器1.Activities.Add(this.goto1);

this.容器1.Activities.Add(this.codeActivity5);

this.容器1.Name = "容器1";

//

// Workflow1

//

this.Activities.Add(this.容器1);

this.Name = "Workflow1";

this.CanModifyActivities = false;

}

private void codeActivity_ExecuteCode(object sender, EventArgs e)

{

System.Console.WriteLine(((Activity)sender).Name);

}

    }

宿主

class Program

{

static void Main()

{

WorkflowRuntime workflowRuntime = new WorkflowRuntime();

workflowRuntime.WorkflowCompleted +=new EventHandler<WorkflowCompletedEventArgs>(workflowRuntime_WorkflowCompleted);

workflowRuntime.WorkflowTerminated +=new EventHandler<WorkflowTerminatedEventArgs>(workflowRuntime_WorkflowTerminated);

workflowRuntime.WorkflowIdled+=new EventHandler<WorkflowEventArgs>(workflowRuntime_WorkflowIdled);

WorkflowInstance instance = workflowRuntime.CreateWorkflow(typeof(Workflow1));

instance.Start();

System.Console.Read();

}

static void workflowRuntime_WorkflowIdled(object sender, WorkflowEventArgs e)

{

System.Console.WriteLine("WorkflowIdled");

}

static void workflowRuntime_WorkflowTerminated(object sender, WorkflowTerminatedEventArgs e)

{

System.Console.WriteLine("Terminated" + e.Exception.Message);

}

static void workflowRuntime_WorkflowCompleted(object sender, WorkflowCompletedEventArgs e)

{

System.Console.WriteLine("WorkflowCompleted");

}

}

运行结果

原文地址:https://www.cnblogs.com/foundation/p/1217343.html