跟着麒麟大神的帖子一步步学习:
http://www.cnblogs.com/zhuqil/archive/2010/04/16/WF4CashRegister.html
构造个Product类
1 public class ProductItem 2 { 3 public decimal ProductPrice { get; set; } 4 public int ProductNumber { get; set; } 5 public string ProductName { get; set; } 6 } 7
构造个Order类
1 public class Order 2 { 3 public string CashMethod{get;set;} 4 public List<ProductItem> ProductList { get; set; } 5 6 public Order() 7 { 8 ProductList = new List<ProductItem>(); 9 } 10 }
WF代码:
1 <Activity mc:Ignorable="sap sap2010 sads" x:Class="WF2Demo.CashRegister3" 2 xmlns="http://schemas.microsoft.com/netfx/2009/xaml/activities" 3 xmlns:local="clr-namespace:WF2Demo" 4 xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 5 xmlns:mca="clr-namespace:Microsoft.CSharp.Activities;assembly=System.Activities" 6 xmlns:sads="http://schemas.microsoft.com/netfx/2010/xaml/activities/debugger" 7 xmlns:sap="http://schemas.microsoft.com/netfx/2009/xaml/activities/presentation" 8 xmlns:sap2010="http://schemas.microsoft.com/netfx/2010/xaml/activities/presentation" 9 xmlns:scg="clr-namespace:System.Collections.Generic;assembly=mscorlib" 10 xmlns:sco="clr-namespace:System.Collections.ObjectModel;assembly=mscorlib" 11 xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"> 12 <x:Members> 13 <x:Property Name="Order" Type="InArgument(local:Order)" /> 14 <x:Property Name="CashMethod" Type="InArgument(x:String)" /> 15 <x:Property Name="totalPrices" Type="OutArgument(x:Decimal)" /> 16 </x:Members> 17 <sap2010:ExpressionActivityEditor.ExpressionActivityEditor>C#</sap2010:ExpressionActivityEditor.ExpressionActivityEditor> 18 <sap2010:WorkflowViewState.IdRef>WF2Demo.CashRegister3_1</sap2010:WorkflowViewState.IdRef> 19 <TextExpression.NamespacesForImplementation> 20 <sco:Collection x:TypeArguments="x:String"> 21 <x:String>System</x:String> 22 <x:String>System.Collections.Generic</x:String> 23 <x:String>System.Data</x:String> 24 <x:String>System.Linq</x:String> 25 <x:String>System.Text</x:String> 26 <x:String>WF2Demo</x:String> 27 </sco:Collection> 28 </TextExpression.NamespacesForImplementation> 29 <TextExpression.ReferencesForImplementation> 30 <sco:Collection x:TypeArguments="AssemblyReference"> 31 <AssemblyReference>Microsoft.CSharp</AssemblyReference> 32 <AssemblyReference>System</AssemblyReference> 33 <AssemblyReference>System.Activities</AssemblyReference> 34 <AssemblyReference>System.Core</AssemblyReference> 35 <AssemblyReference>System.Data</AssemblyReference> 36 <AssemblyReference>System.Runtime.Serialization</AssemblyReference> 37 <AssemblyReference>System.ServiceModel</AssemblyReference> 38 <AssemblyReference>System.ServiceModel.Activities</AssemblyReference> 39 <AssemblyReference>System.Xaml</AssemblyReference> 40 <AssemblyReference>System.Xml</AssemblyReference> 41 <AssemblyReference>System.Xml.Linq</AssemblyReference> 42 <AssemblyReference>mscorlib</AssemblyReference> 43 <AssemblyReference>WF2Demo</AssemblyReference> 44 </sco:Collection> 45 </TextExpression.ReferencesForImplementation> 46 <Sequence sap2010:WorkflowViewState.IdRef="Sequence_1"> 47 <WriteLine sap2010:WorkflowViewState.IdRef="WriteLine_1" Text="WF Begin" /> 48 <ForEach x:TypeArguments="local:ProductItem" DisplayName="ForEach<ProductItem>" sap2010:WorkflowViewState.IdRef="ForEach`1_1"> 49 <ForEach.Values> 50 <InArgument x:TypeArguments="scg:IEnumerable(local:ProductItem)"> 51 <mca:CSharpValue x:TypeArguments="scg:IEnumerable(local:ProductItem)">Order.ProductList</mca:CSharpValue> 52 </InArgument> 53 </ForEach.Values> 54 <ActivityAction x:TypeArguments="local:ProductItem"> 55 <ActivityAction.Argument> 56 <DelegateInArgument x:TypeArguments="local:ProductItem" Name="item" /> 57 </ActivityAction.Argument> 58 <Assign sap2010:WorkflowViewState.IdRef="Assign_1"> 59 <Assign.To> 60 <OutArgument x:TypeArguments="x:Decimal"> 61 <mca:CSharpReference x:TypeArguments="x:Decimal">totalPrices</mca:CSharpReference> 62 </OutArgument> 63 </Assign.To> 64 <Assign.Value> 65 <InArgument x:TypeArguments="x:Decimal"> 66 <mca:CSharpValue x:TypeArguments="x:Decimal">totalPrices + item.ProductNumber * item.ProductPrice</mca:CSharpValue> 67 </InArgument> 68 </Assign.Value> 69 </Assign> 70 </ActivityAction> 71 </ForEach> 72 <Switch x:TypeArguments="x:String" sap2010:WorkflowViewState.IdRef="Switch`1_1"> 73 <Switch.Expression> 74 <InArgument x:TypeArguments="x:String"> 75 <mca:CSharpValue x:TypeArguments="x:String">Order.CashMethod</mca:CSharpValue> 76 </InArgument> 77 </Switch.Expression> 78 <Assign x:Key="打七折" sap2010:WorkflowViewState.IdRef="Assign_2"> 79 <Assign.To> 80 <OutArgument x:TypeArguments="x:Decimal"> 81 <mca:CSharpReference x:TypeArguments="x:Decimal">totalPrices</mca:CSharpReference> 82 </OutArgument> 83 </Assign.To> 84 <Assign.Value> 85 <InArgument x:TypeArguments="x:Decimal"> 86 <mca:CSharpValue x:TypeArguments="x:Decimal">totalPrices*07M</mca:CSharpValue> 87 </InArgument> 88 </Assign.Value> 89 </Assign> 90 <Assign x:Key="打八折" sap2010:WorkflowViewState.IdRef="Assign_3"> 91 <Assign.To> 92 <OutArgument x:TypeArguments="x:Decimal"> 93 <mca:CSharpReference x:TypeArguments="x:Decimal">totalPrices</mca:CSharpReference> 94 </OutArgument> 95 </Assign.To> 96 <Assign.Value> 97 <InArgument x:TypeArguments="x:Decimal"> 98 <mca:CSharpValue x:TypeArguments="x:Decimal">totalPrices*0.8M</mca:CSharpValue> 99 </InArgument> 100 </Assign.Value> 101 </Assign> 102 <If x:Key="满300返100" sap2010:WorkflowViewState.IdRef="If_1"> 103 <If.Condition> 104 <InArgument x:TypeArguments="x:Boolean"> 105 <mca:CSharpValue x:TypeArguments="x:Boolean">totalPrices>=300</mca:CSharpValue> 106 </InArgument> 107 </If.Condition> 108 <If.Then> 109 <Sequence sap2010:WorkflowViewState.IdRef="Sequence_2"> 110 <WriteLine sap2010:WorkflowViewState.IdRef="WriteLine_2" Text="满300返100" /> 111 <Assign sap2010:WorkflowViewState.IdRef="Assign_4"> 112 <Assign.To> 113 <OutArgument x:TypeArguments="x:Decimal"> 114 <mca:CSharpReference x:TypeArguments="x:Decimal">totalPrices</mca:CSharpReference> 115 </OutArgument> 116 </Assign.To> 117 <Assign.Value> 118 <InArgument x:TypeArguments="x:Decimal"> 119 <mca:CSharpValue x:TypeArguments="x:Decimal">totalPrices-100</mca:CSharpValue> 120 </InArgument> 121 </Assign.Value> 122 </Assign> 123 </Sequence> 124 </If.Then> 125 </If> 126 <If x:Key="满200返50" sap2010:WorkflowViewState.IdRef="If_2"> 127 <If.Condition> 128 <InArgument x:TypeArguments="x:Boolean"> 129 <mca:CSharpValue x:TypeArguments="x:Boolean">totalPrices>=200</mca:CSharpValue> 130 </InArgument> 131 </If.Condition> 132 <If.Then> 133 <Sequence sap2010:WorkflowViewState.IdRef="Sequence_3"> 134 <WriteLine sap2010:WorkflowViewState.IdRef="WriteLine_3" Text="满200返50" /> 135 <Assign sap2010:WorkflowViewState.IdRef="Assign_5"> 136 <Assign.To> 137 <OutArgument x:TypeArguments="x:Decimal"> 138 <mca:CSharpReference x:TypeArguments="x:Decimal">totalPrices</mca:CSharpReference> 139 </OutArgument> 140 </Assign.To> 141 <Assign.Value> 142 <InArgument x:TypeArguments="x:Decimal"> 143 <mca:CSharpValue x:TypeArguments="x:Decimal">totalPrices-50</mca:CSharpValue> 144 </InArgument> 145 </Assign.Value> 146 </Assign> 147 </Sequence> 148 </If.Then> 149 </If> 150 </Switch> 151 <sads:DebugSymbol.Symbol>dypEOlxTYWtlXFdGMkRlbW9cV0YyRGVtb1xDYXNoUmVnaXN0ZXIzLnhhbWwgLgOYAQ4CAQEvBS9QAgFLMAVHDwIBO0gFlgEOAgECL0MvTQIBTDMLM3QCAUc6CUUSAgE8SwtLWQIBA04HWRACATJaB2UQAgEpZgd9DAIBGH4HlQEMAgEHQg9CggECAUI9Dz1hAgE9Vg1WWwIBN1ENUV8CATNiDWJcAgEuXQ1dXwIBKmkNaV8CARltC3sWAgEdgQENgQFfAgEIhQELkwEWAgEMbg1uWAIBJ28NehYCAR6GAQ2GAVcCARaHAQ2SARYCAQ1uS25VAgEodxN3YQIBI3ITcmUCAR+GAUuGAVQCARePAROPAWACARKKAROKAWUCAQ4=</sads:DebugSymbol.Symbol> 152 </Sequence> 153 <sap2010:WorkflowViewState.ViewStateManager> 154 <sap2010:ViewStateManager> 155 <sap2010:ViewStateData Id="WriteLine_1" sap:VirtualizedContainerService.HintSize="484,63" /> 156 <sap2010:ViewStateData Id="Assign_1" sap:VirtualizedContainerService.HintSize="243,62" /> 157 <sap2010:ViewStateData Id="ForEach`1_1" sap:VirtualizedContainerService.HintSize="484,214" /> 158 <sap2010:ViewStateData Id="Assign_2" sap:VirtualizedContainerService.HintSize="243,62" /> 159 <sap2010:ViewStateData Id="Assign_3" sap:VirtualizedContainerService.HintSize="243,62" /> 160 <sap2010:ViewStateData Id="WriteLine_2" sap:VirtualizedContainerService.HintSize="243,63" /> 161 <sap2010:ViewStateData Id="Assign_4" sap:VirtualizedContainerService.HintSize="243,62" /> 162 <sap2010:ViewStateData Id="Sequence_2" sap:VirtualizedContainerService.HintSize="265,289"> 163 <sap:WorkflowViewStateService.ViewState> 164 <scg:Dictionary x:TypeArguments="x:String, x:Object"> 165 <x:Boolean x:Key="IsExpanded">True</x:Boolean> 166 </scg:Dictionary> 167 </sap:WorkflowViewStateService.ViewState> 168 </sap2010:ViewStateData> 169 <sap2010:ViewStateData Id="If_1" sap:VirtualizedContainerService.HintSize="464,443" /> 170 <sap2010:ViewStateData Id="WriteLine_3" sap:VirtualizedContainerService.HintSize="243,63" /> 171 <sap2010:ViewStateData Id="Assign_5" sap:VirtualizedContainerService.HintSize="243,62" /> 172 <sap2010:ViewStateData Id="Sequence_3" sap:VirtualizedContainerService.HintSize="265,289"> 173 <sap:WorkflowViewStateService.ViewState> 174 <scg:Dictionary x:TypeArguments="x:String, x:Object"> 175 <x:Boolean x:Key="IsExpanded">True</x:Boolean> 176 </scg:Dictionary> 177 </sap:WorkflowViewStateService.ViewState> 178 </sap2010:ViewStateData> 179 <sap2010:ViewStateData Id="If_2" sap:VirtualizedContainerService.HintSize="464,443" /> 180 <sap2010:ViewStateData Id="Switch`1_1" sap:VirtualizedContainerService.HintSize="484,707" /> 181 <sap2010:ViewStateData Id="Sequence_1" sap:VirtualizedContainerService.HintSize="506,1188"> 182 <sap:WorkflowViewStateService.ViewState> 183 <scg:Dictionary x:TypeArguments="x:String, x:Object"> 184 <x:Boolean x:Key="IsExpanded">True</x:Boolean> 185 </scg:Dictionary> 186 </sap:WorkflowViewStateService.ViewState> 187 </sap2010:ViewStateData> 188 <sap2010:ViewStateData Id="WF2Demo.CashRegister3_1" sap:VirtualizedContainerService.HintSize="546,1268" /> 189 </sap2010:ViewStateManager> 190 </sap2010:WorkflowViewState.ViewStateManager> 191 </Activity>
控制台入口:
1 static void Main(string[] args) 2 { 3 Order myOrder = new Order(); 4 ProductItem item1 = new ProductItem(); 5 item1.ProductName = "苹果"; 6 item1.ProductPrice = 5; 7 item1.ProductNumber = 2; 8 myOrder.ProductList.Add(item1); 9 10 ProductItem item2 = new ProductItem(); 11 item2.ProductName = "外套"; 12 item2.ProductPrice = 200; 13 item2.ProductNumber = 1; 14 myOrder.ProductList.Add(item2); 15 myOrder.CashMethod = "满200返50"; 16 // create dictionary with input arguments for the workflow 17 IDictionary<string, object> input = new Dictionary<string, object> 18 { 19 { "Order" , myOrder } 20 }; 21 22 IDictionary<string, object> output 23 = WorkflowInvoker.Invoke(new CashRegister(), input); 24 decimal total = (decimal)output["totalPrices"]; 25 26 Console.WriteLine("工作流返回的总金额为:" + total.ToString()); 27 28 29 output 30 = WorkflowInvoker.Invoke(new CashRegister1(), input); 31 total = (decimal)output["totalPrices"]; 32 Console.WriteLine("工作流1返回的总金额为:" + total.ToString()); 33 34 35 output 36 = WorkflowInvoker.Invoke(new CashRegister3(), input); 37 total = (decimal)output["totalPrices"]; 38 Console.WriteLine("工作流3返回的总金额为:" + total.ToString()); 39 Console.Read(); 40 }
时间: 2024-10-04 06:09:12