通过AEC解读WF的核心原理(八)实现Switch功能的Activity
说明
例子下载:https://files.cnblogs.com/wxwinter/aec8.rar
本例与IfElseActivity的功能很像,本例的条件判断提供了两种方式,
- Condition方式,这与IfElseActivity的一样,通过[条件分支.规则条件]属性设置
- 提供了与Switch风格的条件判断方式,,通过[条件分支.条件]属性与[条件容器.分支表达式]设置
具体操作见[测试用工作流]的图片说明
本例还演示了使用Designer的技巧:
例子
条件分支Activity
public class 条件分支 : SequenceActivity { public static DependencyProperty 规则条件Property = DependencyProperty.Register("规则条件", typeof(ActivityCondition), typeof(条件分支));
public ActivityCondition 规则条件 { get { return ((ActivityCondition)(base.GetValue(条件分支.规则条件Property))); } set { base.SetValue(条件分支.规则条件Property, value); } }
public static DependencyProperty 条件Property = DependencyProperty.Register("条件", typeof(string), typeof(条件分支), new PropertyMetadata(""));
public string 条件 { get { return ((string)(base.GetValue(条件分支.条件Property))); } set { base.SetValue(条件分支.条件Property, value); } }
} |
条件容器设计器
public class 条件容器设计器 : ParallelActivityDesigner { protected override System.Workflow.ComponentModel.CompositeActivity OnCreateNewBranch() { return new 条件分支(); } } |
条件容器Activity
[Designer(typeof(条件容器设计器), typeof(IDesigner))] public class 条件容器 : CompositeActivity, IActivityEventListener<ActivityExecutionStatusChangedEventArgs> { public 条件容器() { InitializeComponent(); } [System.Diagnostics.DebuggerNonUserCode] private void InitializeComponent() { this.CanModifyActivities = true; this.Name = "条件容器"; this.CanModifyActivities = false; }
public static readonly DependencyProperty 分支表达式Property = DependencyProperty.Register("分支表达式", typeof(string), typeof(条件容器), new PropertyMetadata(""));
public string 分支表达式 { get { return (string)GetValue(分支表达式Property); } set { SetValue(分支表达式Property, value); } }
protected override ActivityExecutionStatus Execute(ActivityExecutionContext executionContext) { foreach (条件分支 branch in EnabledActivities) { if (branch.规则条件 != null) { if (branch.规则条件.Evaluate(branch, executionContext)) { branch.RegisterForStatusChange(Activity.ClosedEvent, this); executionContext.ExecuteActivity(branch); } continue; }
if (branch.条件 == this.分支表达式) { branch.RegisterForStatusChange(Activity.ClosedEvent, this); executionContext.ExecuteActivity(branch); } } return ActivityExecutionStatus.Executing; }
public void OnEvent(object sender, ActivityExecutionStatusChangedEventArgs e) { ActivityExecutionContext aec = sender as ActivityExecutionContext; 条件分支 branch = e.Activity as 条件分支; if (branch != null) { branch.UnregisterForStatusChange(Activity.ClosedEvent, this); }
foreach (Activity child in this.EnabledActivities) { if (child.ExecutionStatus != ActivityExecutionStatus.Closed && child.ExecutionStatus != ActivityExecutionStatus.Initialized) { return; } } aec.CloseActivity(); } } |
测试用工作流
public class Workflow1: SequentialWorkflowActivity { private 条件分支 条件分支2; private 条件分支 条件分支1; private CodeActivity codeActivity3; private CodeActivity codeActivity2; private CodeActivity codeActivity1; private 条件分支 条件分支3; private CodeActivity codeActivity5; private CodeActivity codeActivity4; private 条件分支 条件分支5; private 条件分支 条件分支4; private 条件容器 条件容器2; private 条件容器 条件容器1;
public Workflow1() { InitializeComponent(); } [System.Diagnostics.DebuggerNonUserCode] private void InitializeComponent() { this.CanModifyActivities = true; System.Workflow.Activities.Rules.RuleConditionReference ruleconditionreference1 = new System.Workflow.Activities.Rules.RuleConditionReference(); this.codeActivity5 = new System.Workflow.Activities.CodeActivity(); 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.条件分支5 = new wxwinterAecTest.条件分支(); this.条件分支4 = new wxwinterAecTest.条件分支(); this.条件分支3 = new wxwinterAecTest.条件分支(); this.条件分支2 = new wxwinterAecTest.条件分支(); this.条件分支1 = new wxwinterAecTest.条件分支(); this.条件容器2 = new wxwinterAecTest.条件容器(); this.条件容器1 = new wxwinterAecTest.条件容器(); // // codeActivity5 // this.codeActivity5.Name = "codeActivity5"; this.codeActivity5.ExecuteCode += new System.EventHandler(this.codeActivity5_ExecuteCode); // // codeActivity4 // this.codeActivity4.Name = "codeActivity4"; this.codeActivity4.ExecuteCode += new System.EventHandler(this.codeActivity4_ExecuteCode); // // codeActivity3 // this.codeActivity3.Name = "codeActivity3"; this.codeActivity3.ExecuteCode += new System.EventHandler(this.codeActivity3_ExecuteCode); // // codeActivity2 // this.codeActivity2.Name = "codeActivity2"; this.codeActivity2.ExecuteCode += new System.EventHandler(this.codeActivity2_ExecuteCode); // // codeActivity1 // this.codeActivity1.Name = "codeActivity1"; this.codeActivity1.ExecuteCode += new System.EventHandler(this.codeActivity1_ExecuteCode); // // 条件分支5 // this.条件分支5.Activities.Add(this.codeActivity5); this.条件分支5.Name = "条件分支5"; this.条件分支5.条件 = "wxd"; this.条件分支5.规则条件 = null; // // 条件分支4 // this.条件分支4.Activities.Add(this.codeActivity4); this.条件分支4.Name = "条件分支4"; this.条件分支4.条件 = "lzm"; this.条件分支4.规则条件 = null; // // 条件分支3 // this.条件分支3.Activities.Add(this.codeActivity3); this.条件分支3.Name = "条件分支3"; this.条件分支3.条件 = "lzm"; this.条件分支3.规则条件 = null; // // 条件分支2 // this.条件分支2.Activities.Add(this.codeActivity2); this.条件分支2.Name = "条件分支2"; this.条件分支2.条件 = ""; ruleconditionreference1.ConditionName = "条件2"; this.条件分支2.规则条件 = ruleconditionreference1; // // 条件分支1 // this.条件分支1.Activities.Add(this.codeActivity1); this.条件分支1.Name = "条件分支1"; this.条件分支1.条件 = "wxwinter"; this.条件分支1.规则条件 = null; // // 条件容器2 // this.条件容器2.Activities.Add(this.条件分支4); this.条件容器2.Activities.Add(this.条件分支5); this.条件容器2.Name = "条件容器2"; this.条件容器2.分支表达式 = "lzm"; // // 条件容器1 // this.条件容器1.Activities.Add(this.条件分支1); this.条件容器1.Activities.Add(this.条件分支2); this.条件容器1.Activities.Add(this.条件分支3); this.条件容器1.Name = "条件容器1"; this.条件容器1.分支表达式 = "wxwinter"; // // Workflow1 // this.Activities.Add(this.条件容器1); this.Activities.Add(this.条件容器2); this.Name = "Workflow1"; this.CanModifyActivities = false;
}
private void codeActivity1_ExecuteCode(object sender, EventArgs e) { System.Console.WriteLine("codeActivity1"); }
private void codeActivity2_ExecuteCode(object sender, EventArgs e) { System.Console.WriteLine("codeActivity2"); }
private void codeActivity3_ExecuteCode(object sender, EventArgs e) { System.Console.WriteLine("codeActivity3"); }
private void codeActivity4_ExecuteCode(object sender, EventArgs e) { System.Console.WriteLine("codeActivity4"); }
private void codeActivity5_ExecuteCode(object sender, EventArgs e) { System.Console.WriteLine("codeActivity5"); } } |
<RuleDefinitions xmlns="http://schemas.microsoft.com/winfx/2006/xaml/workflow"> <RuleDefinitions.Conditions> <RuleExpressionCondition Name="条件2"> <RuleExpressionCondition.Expression> <ns0:CodeBinaryOperatorExpression Operator="ValueEquality" xmlns:ns0="clr-namespace:System.CodeDom;Assembly=System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"> <ns0:CodeBinaryOperatorExpression.Left> <ns0:CodePropertyReferenceExpression PropertyName="分支表达式"> <ns0:CodePropertyReferenceExpression.TargetObject> <ns0:CodeFieldReferenceExpression FieldName="条件容器1"> <ns0:CodeFieldReferenceExpression.TargetObject> <ns0:CodeThisReferenceExpression /> </ns0:CodeFieldReferenceExpression.TargetObject> </ns0:CodeFieldReferenceExpression> </ns0:CodePropertyReferenceExpression.TargetObject> </ns0:CodePropertyReferenceExpression> </ns0:CodeBinaryOperatorExpression.Left> <ns0:CodeBinaryOperatorExpression.Right> <ns0:CodePrimitiveExpression> <ns0:CodePrimitiveExpression.Value> <ns1:String xmlns:ns1="clr-namespace:System;Assembly=mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">wxwinter</ns1:String> </ns0:CodePrimitiveExpression.Value> </ns0:CodePrimitiveExpression> </ns0:CodeBinaryOperatorExpression.Right> </ns0:CodeBinaryOperatorExpression> </RuleExpressionCondition.Expression> </RuleExpressionCondition> </RuleDefinitions.Conditions> </RuleDefinitions> |
宿主
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"); }
} |
运行结果