任务 2:创建 PolicyActivity 和 RuleSet
在任务 1:创建 ConditionedActivityGroup 活动中,您将一个 ConditionedActivityGroup 活动添加到一个工作流,该工作流运行它所包含的所有子活动,直到队列中不再留有任何优惠券。 优惠券被使用 ConditionedActivityGroup 中的 CodeActivity 移除。
在本任务中,您将添加单个 PolicyActivity 活动。 此 PolicyActivity 活动使用一个包含四条不同规则的 RuleSet,这些规则分别对应于四个优惠券类型中的每个类型。 当 ConditionedActivityGroup 活动枚举所有优惠券时,每一条规则都将得到计算。 如果规则的 Condition 计算结果为 true,则它将应用与该优惠券关联的逻辑。
创建 PolicyActivity
在 DiscountWorkflow 类中,创建一个名为 couponPolicy 的私有 PolicyActivity。
this.couponPolicy = new System.Workflow.Activities.PolicyActivity();
在 DiscountWorkflow 类的 InitializeComponent 方法中,创建已在上一步中创建的 PolicyActivity 对象的实例。
此操作跟在设置 CanModifyActivities 属性的那一行后面。
System.Workflow.Activities.Rules.RuleSetReference rulesetreference1 = new System.Workflow.Activities.Rules.RuleSetReference();
在 InitializeComponent 方法中创建一个名为 rulesetreference1 的 RuleSetReference 局部变量,然后创建该对象的实例。
this.conditionedActivityGroup1.Activities.Add(this.couponPolicy);
在 InitializeComponent 方法中,使用在 Activities 属性中定义的 Add 方法,将 couponPolicy 对象添加到 conditionedActivityGroup1 对象的 Activities 集合中。
备注
必须在添加 removeCoupon 对象之前添加 couponPolicy 对象。 将子活动添加到 ConditionedActivityGroup 的顺序决定了它们的运行顺序。
// // couponPolicy // this.couponPolicy.Name = "couponPolicy"; rulesetreference1.RuleSetName = "RuleSet1"; this.couponPolicy.RuleSetReference = rulesetreference1;
将 couponPolicy 对象的 Name 属性设置为值 "couponPolicy"。
将 rulesetreference1 对象的 RuleSetName 设置为值 "RuleSet1"。
备注
下一过程中将创建此 RuleSet。
将 couponPolicy 对象的 RuleSetReference 属性设置为等于 rulesetreference1。 这会将该 PolicyActivity 与包含用于处理优惠券的规则的 RuleSet 关联。
this.couponPolicy.SetValue( System.Workflow.Activities.ConditionedActivityGroup.WhenConditionProperty , ruleconditionreference3);
调用在 couponPolicy 对象中定义的 SetValue 方法,并且将 WhenConditionProperty 类型和 ruleconditionreference3 对象作为参数传入。
备注
通过设置 PolicyActivity 活动的 WhenConditionProperty,可以保证在每次 ConditionedActivityGroup 活动枚举其子活动时,PolicyActivity 活动都会运行。 如果没有为 ConditionedActivityGroup 中的子活动指定 WhenConditionProperty 条件,则该子活动只运行一次。
创建优惠券规则集
在 DiscountWorkflow.rules 文件中,将一个名为 RuleDefinitions.RuleSets 的新元素添加到 RuleDefinitions 根元素。
将一个名为 RuleSet 的子元素添加到 RuleDefinitions.RuleSets 元素。
将一个名为 Name 的属性添加到在上一步中创建的 RuleSet 元素,并将值“RuleSet1”赋给该属性。
将一个名为 ChainingBehavior 的属性添加到在步骤 3 中创建的 RuleSet 元素,并将值“Full”赋给该属性。
将一个名为 Description 的属性添加到在步骤 3 中创建的 RuleSet 元素,并将值“Coupon RuleSet”赋给该属性。
将一个名为 RuleSet.Rules 的子元素添加到 RuleSet 元素。
您的 DiscountWorkflow.rules 文件将类似于下面的代码。
<RuleDefinitions xmlns="https://schemas.microsoft.com/winfx/2006/xaml/workflow"> <RuleDefinitions.Conditions> <!-- Conditions hidden for this example for readability --> </RuleDefinitions.Conditions> <RuleDefinitions.RuleSets> <RuleSet Name="RuleSet1" ChainingBehavior="Full" Description="Coupon RuleSet"> <RuleSet.Rules> </RuleSet.Rules> </RuleSet> </RuleDefinitions.RuleSets> </RuleDefinitions>
创建 getFreeItems 规则
将下面的 Rule 复制到在上一个过程中创建的 RuleSet.Rules 元素。
<Rule Name="getFreeItem" ReevaluationBehavior="Always" Priority="0" Description="{p5:Null}" Active="True" xmlns:p5="https://schemas.microsoft.com/winfx/2006/xaml"> <Rule.ThenActions> <RuleStatementAction> <RuleStatementAction.CodeDomStatement> <ns0:CodeAssignStatement LinePragma="{p5:Null}" xmlns:ns0="clr-namespace:System.CodeDom;Assembly=System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"> <ns0:CodeAssignStatement.Left> <ns0:CodePropertyReferenceExpression PropertyName="Discount"> <ns0:CodePropertyReferenceExpression.TargetObject> <ns0:CodeThisReferenceExpression /> </ns0:CodePropertyReferenceExpression.TargetObject> </ns0:CodePropertyReferenceExpression> </ns0:CodeAssignStatement.Left> <ns0:CodeAssignStatement.Right> <ns0:CodeBinaryOperatorExpression Operator="Add"> <ns0:CodeBinaryOperatorExpression.Left> <ns0:CodePropertyReferenceExpression PropertyName="Discount"> <ns0:CodePropertyReferenceExpression.TargetObject> <ns0:CodeThisReferenceExpression /> </ns0:CodePropertyReferenceExpression.TargetObject> </ns0:CodePropertyReferenceExpression> </ns0:CodeBinaryOperatorExpression.Left> <ns0:CodeBinaryOperatorExpression.Right> <ns0:CodeFieldReferenceExpression FieldName="price"> <ns0:CodeFieldReferenceExpression.TargetObject> <ns0:CodeIndexerExpression> <ns0:CodeIndexerExpression.Indices> <ns0:CodePrimitiveExpression> <ns0:CodePrimitiveExpression.Value> <ns1:Int32 xmlns:ns1="clr-namespace:System;Assembly=mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">0</ns1:Int32> </ns0:CodePrimitiveExpression.Value> </ns0:CodePrimitiveExpression> </ns0:CodeIndexerExpression.Indices> <ns0:CodeIndexerExpression.TargetObject> <ns0:CodePropertyReferenceExpression PropertyName="Items"> <ns0:CodePropertyReferenceExpression.TargetObject> <ns0:CodeThisReferenceExpression /> </ns0:CodePropertyReferenceExpression.TargetObject> </ns0:CodePropertyReferenceExpression> </ns0:CodeIndexerExpression.TargetObject> </ns0:CodeIndexerExpression> </ns0:CodeFieldReferenceExpression.TargetObject> </ns0:CodeFieldReferenceExpression> </ns0:CodeBinaryOperatorExpression.Right> </ns0:CodeBinaryOperatorExpression> </ns0:CodeAssignStatement.Right> </ns0:CodeAssignStatement> </RuleStatementAction.CodeDomStatement> </RuleStatementAction> </Rule.ThenActions> <Rule.Condition> <RuleExpressionCondition Name="{p5:Null}"> <RuleExpressionCondition.Expression> <ns0:CodeBinaryOperatorExpression Operator="BooleanAnd" xmlns:ns0="clr-namespace:System.CodeDom;Assembly=System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"> <ns0:CodeBinaryOperatorExpression.Left> <ns0:CodeBinaryOperatorExpression Operator="ValueEquality"> <ns0:CodeBinaryOperatorExpression.Left> <ns0:CodeFieldReferenceExpression FieldName="type"> <ns0:CodeFieldReferenceExpression.TargetObject> <ns0:CodeMethodInvokeExpression> <ns0:CodeMethodInvokeExpression.Method> <ns0:CodeMethodReferenceExpression MethodName="Peek"> <ns0:CodeMethodReferenceExpression.TargetObject> <ns0:CodePropertyReferenceExpression PropertyName="Coupons"> <ns0:CodePropertyReferenceExpression.TargetObject> <ns0:CodeThisReferenceExpression /> </ns0:CodePropertyReferenceExpression.TargetObject> </ns0:CodePropertyReferenceExpression> </ns0:CodeMethodReferenceExpression.TargetObject> </ns0:CodeMethodReferenceExpression> </ns0:CodeMethodInvokeExpression.Method> </ns0:CodeMethodInvokeExpression> </ns0:CodeFieldReferenceExpression.TargetObject> </ns0:CodeFieldReferenceExpression> </ns0:CodeBinaryOperatorExpression.Left> <ns0:CodeBinaryOperatorExpression.Right> <ns0:CodeFieldReferenceExpression FieldName="FreeItem"> <ns0:CodeFieldReferenceExpression.TargetObject> <ns0:CodeTypeReferenceExpression Type="Microsoft.Samples.Workflow.Tutorials.RulesAndConditions.DiscountWorkflow+CouponType" /> </ns0:CodeFieldReferenceExpression.TargetObject> </ns0:CodeFieldReferenceExpression> </ns0:CodeBinaryOperatorExpression.Right> </ns0:CodeBinaryOperatorExpression> </ns0:CodeBinaryOperatorExpression.Left> <ns0:CodeBinaryOperatorExpression.Right> <ns0:CodeBinaryOperatorExpression Operator="GreaterThanOrEqual"> <ns0:CodeBinaryOperatorExpression.Left> <ns0:CodePropertyReferenceExpression PropertyName="Count"> <ns0:CodePropertyReferenceExpression.TargetObject> <ns0:CodePropertyReferenceExpression PropertyName="Items"> <ns0:CodePropertyReferenceExpression.TargetObject> <ns0:CodeThisReferenceExpression /> </ns0:CodePropertyReferenceExpression.TargetObject> </ns0:CodePropertyReferenceExpression> </ns0:CodePropertyReferenceExpression.TargetObject> </ns0:CodePropertyReferenceExpression> </ns0:CodeBinaryOperatorExpression.Left> <ns0:CodeBinaryOperatorExpression.Right> <ns0:CodePrimitiveExpression> <ns0:CodePrimitiveExpression.Value> <ns1:Int32 xmlns:ns1="clr-namespace:System;Assembly=mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">5</ns1:Int32> </ns0:CodePrimitiveExpression.Value> </ns0:CodePrimitiveExpression> </ns0:CodeBinaryOperatorExpression.Right> </ns0:CodeBinaryOperatorExpression> </ns0:CodeBinaryOperatorExpression.Right> </ns0:CodeBinaryOperatorExpression> </RuleExpressionCondition.Expression> </RuleExpressionCondition> </Rule.Condition> </Rule>
创建 applyPercentageOffHighest 规则
将下面的 Rule 复制到在上一个过程中创建的 RuleSet.Rules 元素。
<Rule Name="applyPercentageOffHighest" ReevaluationBehavior="Always" Priority="0" Description="{p5:Null}" Active="True" xmlns:p5="https://schemas.microsoft.com/winfx/2006/xaml"> <Rule.ThenActions> <RuleStatementAction> <RuleStatementAction.CodeDomStatement> <ns0:CodeAssignStatement LinePragma="{p5:Null}" xmlns:ns0="clr-namespace:System.CodeDom;Assembly=System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"> <ns0:CodeAssignStatement.Left> <ns0:CodePropertyReferenceExpression PropertyName="Discount"> <ns0:CodePropertyReferenceExpression.TargetObject> <ns0:CodeThisReferenceExpression /> </ns0:CodePropertyReferenceExpression.TargetObject> </ns0:CodePropertyReferenceExpression> </ns0:CodeAssignStatement.Left> <ns0:CodeAssignStatement.Right> <ns0:CodeBinaryOperatorExpression Operator="Add"> <ns0:CodeBinaryOperatorExpression.Left> <ns0:CodePropertyReferenceExpression PropertyName="Discount"> <ns0:CodePropertyReferenceExpression.TargetObject> <ns0:CodeThisReferenceExpression /> </ns0:CodePropertyReferenceExpression.TargetObject> </ns0:CodePropertyReferenceExpression> </ns0:CodeBinaryOperatorExpression.Left> <ns0:CodeBinaryOperatorExpression.Right> <ns0:CodeBinaryOperatorExpression Operator="Multiply"> <ns0:CodeBinaryOperatorExpression.Left> <ns0:CodeFieldReferenceExpression FieldName="price"> <ns0:CodeFieldReferenceExpression.TargetObject> <ns0:CodeIndexerExpression> <ns0:CodeIndexerExpression.Indices> <ns0:CodeBinaryOperatorExpression Operator="Subtract"> <ns0:CodeBinaryOperatorExpression.Left> <ns0:CodePropertyReferenceExpression PropertyName="Count"> <ns0:CodePropertyReferenceExpression.TargetObject> <ns0:CodePropertyReferenceExpression PropertyName="Items"> <ns0:CodePropertyReferenceExpression.TargetObject> <ns0:CodeThisReferenceExpression /> </ns0:CodePropertyReferenceExpression.TargetObject> </ns0:CodePropertyReferenceExpression> </ns0:CodePropertyReferenceExpression.TargetObject> </ns0:CodePropertyReferenceExpression> </ns0:CodeBinaryOperatorExpression.Left> <ns0:CodeBinaryOperatorExpression.Right> <ns0:CodePrimitiveExpression> <ns0:CodePrimitiveExpression.Value> <ns1:Int32 xmlns:ns1="clr-namespace:System;Assembly=mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">1</ns1:Int32> </ns0:CodePrimitiveExpression.Value> </ns0:CodePrimitiveExpression> </ns0:CodeBinaryOperatorExpression.Right> </ns0:CodeBinaryOperatorExpression> </ns0:CodeIndexerExpression.Indices> <ns0:CodeIndexerExpression.TargetObject> <ns0:CodePropertyReferenceExpression PropertyName="Items"> <ns0:CodePropertyReferenceExpression.TargetObject> <ns0:CodeThisReferenceExpression /> </ns0:CodePropertyReferenceExpression.TargetObject> </ns0:CodePropertyReferenceExpression> </ns0:CodeIndexerExpression.TargetObject> </ns0:CodeIndexerExpression> </ns0:CodeFieldReferenceExpression.TargetObject> </ns0:CodeFieldReferenceExpression> </ns0:CodeBinaryOperatorExpression.Left> <ns0:CodeBinaryOperatorExpression.Right> <ns0:CodePrimitiveExpression> <ns0:CodePrimitiveExpression.Value> <ns1:Double xmlns:ns1="clr-namespace:System;Assembly=mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">0.1</ns1:Double> </ns0:CodePrimitiveExpression.Value> </ns0:CodePrimitiveExpression> </ns0:CodeBinaryOperatorExpression.Right> </ns0:CodeBinaryOperatorExpression> </ns0:CodeBinaryOperatorExpression.Right> </ns0:CodeBinaryOperatorExpression> </ns0:CodeAssignStatement.Right> </ns0:CodeAssignStatement> </RuleStatementAction.CodeDomStatement> </RuleStatementAction> </Rule.ThenActions> <Rule.Condition> <RuleExpressionCondition Name="{p5:Null}"> <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:CodeFieldReferenceExpression FieldName="type"> <ns0:CodeFieldReferenceExpression.TargetObject> <ns0:CodeMethodInvokeExpression> <ns0:CodeMethodInvokeExpression.Method> <ns0:CodeMethodReferenceExpression MethodName="Peek"> <ns0:CodeMethodReferenceExpression.TargetObject> <ns0:CodePropertyReferenceExpression PropertyName="Coupons"> <ns0:CodePropertyReferenceExpression.TargetObject> <ns0:CodeThisReferenceExpression /> </ns0:CodePropertyReferenceExpression.TargetObject> </ns0:CodePropertyReferenceExpression> </ns0:CodeMethodReferenceExpression.TargetObject> </ns0:CodeMethodReferenceExpression> </ns0:CodeMethodInvokeExpression.Method> </ns0:CodeMethodInvokeExpression> </ns0:CodeFieldReferenceExpression.TargetObject> </ns0:CodeFieldReferenceExpression> </ns0:CodeBinaryOperatorExpression.Left> <ns0:CodeBinaryOperatorExpression.Right> <ns0:CodeFieldReferenceExpression FieldName="PercentageOffHighest"> <ns0:CodeFieldReferenceExpression.TargetObject> <ns0:CodeTypeReferenceExpression Type="Microsoft.Samples.Workflow.Tutorials.RulesAndConditions.DiscountWorkflow+CouponType" /> </ns0:CodeFieldReferenceExpression.TargetObject> </ns0:CodeFieldReferenceExpression> </ns0:CodeBinaryOperatorExpression.Right> </ns0:CodeBinaryOperatorExpression> </RuleExpressionCondition.Expression> </RuleExpressionCondition> </Rule.Condition> </Rule>
过程标题
将下面的 Rule 复制到在上一个过程中创建的 RuleSet.Rules 元素。
<Rule Name="applyPercentageOffLowest" ReevaluationBehavior="Always" Priority="0" Description="{p5:Null}" Active="True" xmlns:p5="https://schemas.microsoft.com/winfx/2006/xaml"> <Rule.ThenActions> <RuleStatementAction> <RuleStatementAction.CodeDomStatement> <ns0:CodeAssignStatement LinePragma="{p5:Null}" xmlns:ns0="clr-namespace:System.CodeDom;Assembly=System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"> <ns0:CodeAssignStatement.Left> <ns0:CodePropertyReferenceExpression PropertyName="Discount"> <ns0:CodePropertyReferenceExpression.TargetObject> <ns0:CodeThisReferenceExpression /> </ns0:CodePropertyReferenceExpression.TargetObject> </ns0:CodePropertyReferenceExpression> </ns0:CodeAssignStatement.Left> <ns0:CodeAssignStatement.Right> <ns0:CodeBinaryOperatorExpression Operator="Add"> <ns0:CodeBinaryOperatorExpression.Left> <ns0:CodePropertyReferenceExpression PropertyName="Discount"> <ns0:CodePropertyReferenceExpression.TargetObject> <ns0:CodeThisReferenceExpression /> </ns0:CodePropertyReferenceExpression.TargetObject> </ns0:CodePropertyReferenceExpression> </ns0:CodeBinaryOperatorExpression.Left> <ns0:CodeBinaryOperatorExpression.Right> <ns0:CodeBinaryOperatorExpression Operator="Multiply"> <ns0:CodeBinaryOperatorExpression.Left> <ns0:CodeFieldReferenceExpression FieldName="price"> <ns0:CodeFieldReferenceExpression.TargetObject> <ns0:CodeIndexerExpression> <ns0:CodeIndexerExpression.Indices> <ns0:CodePrimitiveExpression> <ns0:CodePrimitiveExpression.Value> <ns1:Int32 xmlns:ns1="clr-namespace:System;Assembly=mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">0</ns1:Int32> </ns0:CodePrimitiveExpression.Value> </ns0:CodePrimitiveExpression> </ns0:CodeIndexerExpression.Indices> <ns0:CodeIndexerExpression.TargetObject> <ns0:CodePropertyReferenceExpression PropertyName="Items"> <ns0:CodePropertyReferenceExpression.TargetObject> <ns0:CodeThisReferenceExpression /> </ns0:CodePropertyReferenceExpression.TargetObject> </ns0:CodePropertyReferenceExpression> </ns0:CodeIndexerExpression.TargetObject> </ns0:CodeIndexerExpression> </ns0:CodeFieldReferenceExpression.TargetObject> </ns0:CodeFieldReferenceExpression> </ns0:CodeBinaryOperatorExpression.Left> <ns0:CodeBinaryOperatorExpression.Right> <ns0:CodePrimitiveExpression> <ns0:CodePrimitiveExpression.Value> <ns1:Double xmlns:ns1="clr-namespace:System;Assembly=mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">0.15</ns1:Double> </ns0:CodePrimitiveExpression.Value> </ns0:CodePrimitiveExpression> </ns0:CodeBinaryOperatorExpression.Right> </ns0:CodeBinaryOperatorExpression> </ns0:CodeBinaryOperatorExpression.Right> </ns0:CodeBinaryOperatorExpression> </ns0:CodeAssignStatement.Right> </ns0:CodeAssignStatement> </RuleStatementAction.CodeDomStatement> </RuleStatementAction> </Rule.ThenActions> <Rule.Condition> <RuleExpressionCondition Name="{p5:Null}"> <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:CodeFieldReferenceExpression FieldName="type"> <ns0:CodeFieldReferenceExpression.TargetObject> <ns0:CodeMethodInvokeExpression> <ns0:CodeMethodInvokeExpression.Method> <ns0:CodeMethodReferenceExpression MethodName="Peek"> <ns0:CodeMethodReferenceExpression.TargetObject> <ns0:CodePropertyReferenceExpression PropertyName="Coupons"> <ns0:CodePropertyReferenceExpression.TargetObject> <ns0:CodeThisReferenceExpression /> </ns0:CodePropertyReferenceExpression.TargetObject> </ns0:CodePropertyReferenceExpression> </ns0:CodeMethodReferenceExpression.TargetObject> </ns0:CodeMethodReferenceExpression> </ns0:CodeMethodInvokeExpression.Method> </ns0:CodeMethodInvokeExpression> </ns0:CodeFieldReferenceExpression.TargetObject> </ns0:CodeFieldReferenceExpression> </ns0:CodeBinaryOperatorExpression.Left> <ns0:CodeBinaryOperatorExpression.Right> <ns0:CodeFieldReferenceExpression FieldName="PercentageOffLowest"> <ns0:CodeFieldReferenceExpression.TargetObject> <ns0:CodeTypeReferenceExpression Type="Microsoft.Samples.Workflow.Tutorials.RulesAndConditions.DiscountWorkflow+CouponType" /> </ns0:CodeFieldReferenceExpression.TargetObject> </ns0:CodeFieldReferenceExpression> </ns0:CodeBinaryOperatorExpression.Right> </ns0:CodeBinaryOperatorExpression> </RuleExpressionCondition.Expression> </RuleExpressionCondition> </Rule.Condition> </Rule>
创建 applyPercentageOffLowest 规则
将下面的 Rule 复制到在上一个过程中创建的 RuleSet.Rules 元素。
<Rule Name="applyPercentageOffLowest" ReevaluationBehavior="Always" Priority="0" Description="{p5:Null}" Active="True" xmlns:p5="https://schemas.microsoft.com/winfx/2006/xaml"> <Rule.ThenActions> <RuleStatementAction> <RuleStatementAction.CodeDomStatement> <ns0:CodeAssignStatement LinePragma="{p5:Null}" xmlns:ns0="clr-namespace:System.CodeDom;Assembly=System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"> <ns0:CodeAssignStatement.Left> <ns0:CodePropertyReferenceExpression PropertyName="Discount"> <ns0:CodePropertyReferenceExpression.TargetObject> <ns0:CodeThisReferenceExpression /> </ns0:CodePropertyReferenceExpression.TargetObject> </ns0:CodePropertyReferenceExpression> </ns0:CodeAssignStatement.Left> <ns0:CodeAssignStatement.Right> <ns0:CodeBinaryOperatorExpression Operator="Add"> <ns0:CodeBinaryOperatorExpression.Left> <ns0:CodePropertyReferenceExpression PropertyName="Discount"> <ns0:CodePropertyReferenceExpression.TargetObject> <ns0:CodeThisReferenceExpression /> </ns0:CodePropertyReferenceExpression.TargetObject> </ns0:CodePropertyReferenceExpression> </ns0:CodeBinaryOperatorExpression.Left> <ns0:CodeBinaryOperatorExpression.Right> <ns0:CodeBinaryOperatorExpression Operator="Multiply"> <ns0:CodeBinaryOperatorExpression.Left> <ns0:CodeFieldReferenceExpression FieldName="price"> <ns0:CodeFieldReferenceExpression.TargetObject> <ns0:CodeIndexerExpression> <ns0:CodeIndexerExpression.Indices> <ns0:CodePrimitiveExpression> <ns0:CodePrimitiveExpression.Value> <ns1:Int32 xmlns:ns1="clr-namespace:System;Assembly=mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">0</ns1:Int32> </ns0:CodePrimitiveExpression.Value> </ns0:CodePrimitiveExpression> </ns0:CodeIndexerExpression.Indices> <ns0:CodeIndexerExpression.TargetObject> <ns0:CodePropertyReferenceExpression PropertyName="Items"> <ns0:CodePropertyReferenceExpression.TargetObject> <ns0:CodeThisReferenceExpression /> </ns0:CodePropertyReferenceExpression.TargetObject> </ns0:CodePropertyReferenceExpression> </ns0:CodeIndexerExpression.TargetObject> </ns0:CodeIndexerExpression> </ns0:CodeFieldReferenceExpression.TargetObject> </ns0:CodeFieldReferenceExpression> </ns0:CodeBinaryOperatorExpression.Left> <ns0:CodeBinaryOperatorExpression.Right> <ns0:CodePrimitiveExpression> <ns0:CodePrimitiveExpression.Value> <ns1:Double xmlns:ns1="clr-namespace:System;Assembly=mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">0.15</ns1:Double> </ns0:CodePrimitiveExpression.Value> </ns0:CodePrimitiveExpression> </ns0:CodeBinaryOperatorExpression.Right> </ns0:CodeBinaryOperatorExpression> </ns0:CodeBinaryOperatorExpression.Right> </ns0:CodeBinaryOperatorExpression> </ns0:CodeAssignStatement.Right> </ns0:CodeAssignStatement> </RuleStatementAction.CodeDomStatement> </RuleStatementAction> </Rule.ThenActions> <Rule.Condition> <RuleExpressionCondition Name="{p5:Null}"> <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:CodeFieldReferenceExpression FieldName="type"> <ns0:CodeFieldReferenceExpression.TargetObject> <ns0:CodeMethodInvokeExpression> <ns0:CodeMethodInvokeExpression.Method> <ns0:CodeMethodReferenceExpression MethodName="Peek"> <ns0:CodeMethodReferenceExpression.TargetObject> <ns0:CodePropertyReferenceExpression PropertyName="Coupons"> <ns0:CodePropertyReferenceExpression.TargetObject> <ns0:CodeThisReferenceExpression /> </ns0:CodePropertyReferenceExpression.TargetObject> </ns0:CodePropertyReferenceExpression> </ns0:CodeMethodReferenceExpression.TargetObject> </ns0:CodeMethodReferenceExpression> </ns0:CodeMethodInvokeExpression.Method> </ns0:CodeMethodInvokeExpression> </ns0:CodeFieldReferenceExpression.TargetObject> </ns0:CodeFieldReferenceExpression> </ns0:CodeBinaryOperatorExpression.Left> <ns0:CodeBinaryOperatorExpression.Right> <ns0:CodeFieldReferenceExpression FieldName="PercentageOffLowest"> <ns0:CodeFieldReferenceExpression.TargetObject> <ns0:CodeTypeReferenceExpression Type="Microsoft.Samples.Workflow.Tutorials.RulesAndConditions.DiscountWorkflow+CouponType" /> </ns0:CodeFieldReferenceExpression.TargetObject> </ns0:CodeFieldReferenceExpression> </ns0:CodeBinaryOperatorExpression.Right> </ns0:CodeBinaryOperatorExpression> </RuleExpressionCondition.Expression> </RuleExpressionCondition> </Rule.Condition> </Rule>
创建 applyPercentageOffTotal 规则
将下面的 Rule 复制到在上一个过程中创建的 RuleSet.Rules 元素。
<Rule Name="applyPercentageOffTotal" ReevaluationBehavior="Always" Priority="0" Description="{p5:Null}" Active="True" xmlns:p5="https://schemas.microsoft.com/winfx/2006/xaml"> <Rule.ThenActions> <RuleStatementAction> <RuleStatementAction.CodeDomStatement> <ns0:CodeAssignStatement LinePragma="{p5:Null}" xmlns:ns0="clr-namespace:System.CodeDom;Assembly=System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"> <ns0:CodeAssignStatement.Left> <ns0:CodePropertyReferenceExpression PropertyName="Discount"> <ns0:CodePropertyReferenceExpression.TargetObject> <ns0:CodeThisReferenceExpression /> </ns0:CodePropertyReferenceExpression.TargetObject> </ns0:CodePropertyReferenceExpression> </ns0:CodeAssignStatement.Left> <ns0:CodeAssignStatement.Right> <ns0:CodeBinaryOperatorExpression Operator="Add"> <ns0:CodeBinaryOperatorExpression.Left> <ns0:CodePropertyReferenceExpression PropertyName="Discount"> <ns0:CodePropertyReferenceExpression.TargetObject> <ns0:CodeThisReferenceExpression /> </ns0:CodePropertyReferenceExpression.TargetObject> </ns0:CodePropertyReferenceExpression> </ns0:CodeBinaryOperatorExpression.Left> <ns0:CodeBinaryOperatorExpression.Right> <ns0:CodeBinaryOperatorExpression Operator="Multiply"> <ns0:CodeBinaryOperatorExpression.Left> <ns0:CodePropertyReferenceExpression PropertyName="SubTotal"> <ns0:CodePropertyReferenceExpression.TargetObject> <ns0:CodeThisReferenceExpression /> </ns0:CodePropertyReferenceExpression.TargetObject> </ns0:CodePropertyReferenceExpression> </ns0:CodeBinaryOperatorExpression.Left> <ns0:CodeBinaryOperatorExpression.Right> <ns0:CodePrimitiveExpression> <ns0:CodePrimitiveExpression.Value> <ns1:Double xmlns:ns1="clr-namespace:System;Assembly=mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">0.2</ns1:Double> </ns0:CodePrimitiveExpression.Value> </ns0:CodePrimitiveExpression> </ns0:CodeBinaryOperatorExpression.Right> </ns0:CodeBinaryOperatorExpression> </ns0:CodeBinaryOperatorExpression.Right> </ns0:CodeBinaryOperatorExpression> </ns0:CodeAssignStatement.Right> </ns0:CodeAssignStatement> </RuleStatementAction.CodeDomStatement> </RuleStatementAction> </Rule.ThenActions> <Rule.Condition> <RuleExpressionCondition Name="{p5:Null}"> <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:CodeFieldReferenceExpression FieldName="type"> <ns0:CodeFieldReferenceExpression.TargetObject> <ns0:CodeMethodInvokeExpression> <ns0:CodeMethodInvokeExpression.Method> <ns0:CodeMethodReferenceExpression MethodName="Peek"> <ns0:CodeMethodReferenceExpression.TargetObject> <ns0:CodePropertyReferenceExpression PropertyName="Coupons"> <ns0:CodePropertyReferenceExpression.TargetObject> <ns0:CodeThisReferenceExpression /> </ns0:CodePropertyReferenceExpression.TargetObject> </ns0:CodePropertyReferenceExpression> </ns0:CodeMethodReferenceExpression.TargetObject> </ns0:CodeMethodReferenceExpression> </ns0:CodeMethodInvokeExpression.Method> </ns0:CodeMethodInvokeExpression> </ns0:CodeFieldReferenceExpression.TargetObject> </ns0:CodeFieldReferenceExpression> </ns0:CodeBinaryOperatorExpression.Left> <ns0:CodeBinaryOperatorExpression.Right> <ns0:CodeFieldReferenceExpression FieldName="PercentageOffTotal"> <ns0:CodeFieldReferenceExpression.TargetObject> <ns0:CodeTypeReferenceExpression Type="Microsoft.Samples.Workflow.Tutorials.RulesAndConditions.DiscountWorkflow+CouponType" /> </ns0:CodeFieldReferenceExpression.TargetObject> </ns0:CodeFieldReferenceExpression> </ns0:CodeBinaryOperatorExpression.Right> </ns0:CodeBinaryOperatorExpression> </RuleExpressionCondition.Expression> </RuleExpressionCondition> </Rule.Condition> </Rule>
编译代码
有关编译代码的信息,请参见编译代码。
有关本教程的完成版本,请参见完整的规则和条件教程。
请参见
参考
ConditionedActivityGroup
UntilCondition
RuleConditionReference
ConditionName
Activities
WhenConditionProperty
SetValue
其他资源
IfElse With Rules
Rules and Conditions
版权所有 (C) 2007 Microsoft Corporation。保留所有权利。