WF4B1 的Procedural Activity 之Assign,WriteLine,IF,While,DoWhile,Switch<T>使用

目录

1.1    Assign 赋值    1

1.1.1    参数,变量赋值    1

1.1.2        1

1.2    WriteLine 输出    2

1.2.1    Text属性    2

1.2.2    TextWriter 属性    2

1.2.3        3

1.3    IF 条件    3

1.3.1    例    4

1.4    While 条件循环    4

1.4.1    例    5

1.5    DoWhile 条件循环    6

1.6    Switch<T> 分支选择    6

1.6.1    例    7

Assign 赋值

类名:System.Activities.Statements.Assign

基类:CodeActivity

文件: System.Activities.dll

类型:sealed

说明: 1.可以对流程内定义的参数,变量赋值

2.

参数,变量赋值

  • To 属性 : 要赋值的对象
  • Value 属性 : 要赋的值,可以使用表达式方式

1.定义In参数[myInput]与Out参数[myOutpot]

2.启动流程时传入[myInput]的值,

3.在流程中用[Assign]将[myInput]的值加上["wxwinter"]并赋给[myOutpot],

4.在流程完成事件中读取[myOutpot]的值

流程

<p:Activity mc:Ignorable=""

x:Class="myArgumentsTest.Sequence1"

xmlns="http://schemas.microsoft.com/netfx/2009/xaml/activities/design"

xmlns:__Sequence1="clr-namespace:myArgumentsTest;"

xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"

xmlns:p="http://schemas.microsoft.com/netfx/2009/xaml/activities"

xmlns:sad="clr-namespace:System.Activities.Debugger;assembly=System.Activities"

xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">

<x:Members>

<x:Property Name="myInput" Type="p:InArgument(x:String)" />

<x:Property Name="myOutpot" Type="p:OutArgument(x:String)" />

</x:Members>

<p:Sequence>

<p:Assign>

<p:Assign.To>

<p:OutArgument x:TypeArguments="x:String">[myOutput]</p:OutArgument>

</p:Assign.To>

<p:Assign.Value>

<p:InArgument x:TypeArguments="x:String">[myInput + "wxwinter"]</p:InArgument>

</p:Assign.Value>

</p:Assign>

</p:Sequence>

</p:Activity> 

宿主

class Program

{

static void Main(string[] args)

{

Dictionary<string, object> inputCollection = new Dictionary<string, object>();

inputCollection.Add("myInput", "wxd");

WorkflowInstance myInstance = new WorkflowInstance(new Sequence1(),inputCollection);

myInstance.OnCompleted += completed;

myInstance.Run();

System.Console.Read();

}

static void completed(WorkflowCompletedEventArgs e)

{

System.Console.WriteLine(e.Outputs["myOutput"].ToString());

}

} 

结果

WriteLine 输出

类名:System.Activities.Statements.WriteLine

基类:CodeActivity

文件: System.Activities.dll

类型:sealed

说明:1.调用[TextWriter 属性]所指定的[System.IO.TextWriter 对像]输出[Text 属性] 的值

2.如果没指定[TextWriter 属性],默认为控制台输出[Text 属性] 的值

Text属性

要输出的值,可以使用表达式

TextWriter 属性

System.Activities.InArgument<TextWriter>

自定义

TextWriter 

public class myTextWriter : System.IO.TextWriter

{

public override Encoding Encoding

{

get { return Encoding.UTF8; }

}

public override void WriteLine(string value)

{

System.Console.WriteLine("wxwinter:" + value);

}

} 

使用

xaml

<p:WriteLine TextWriter="[New test.myTextWriter()]">["wxd"]</p:WriteLine>

效果

1.定义In参数[myInput]

2.启动流程时传入[myInput]的值

3.在流程中用[WriteLine]将[myInput]的值以格式化的方式在屏幕打印

流程

<p:Activity mc:Ignorable="" x:Class="myArgumentsTest.Sequence1"

xmlns="http://schemas.microsoft.com/netfx/2009/xaml/activities/design"

xmlns:__Sequence1="clr-namespace:myArgumentsTest;"

xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"

xmlns:p="http://schemas.microsoft.com/netfx/2009/xaml/activities" xmlns:sad="clr-namespace:System.Activities.Debugger;assembly=System.Activities"

xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">

<x:Members>

<x:Property Name="myInput" Type="p:InArgument(x:String)" />

</x:Members>

<p:Sequence >

<p:WriteLine>[String.Format("myInput值:{0}", myInput)]</p:WriteLine>

</p:Sequence>

</p:Activity> 

宿主

class Program

{

static void Main(string[] args)

{

Dictionary<string, object> inputCollection = new Dictionary<string, object>();

inputCollection.Add("myInput", "wxd");

WorkflowInstance myInstance = new WorkflowInstance(new Sequence1(),inputCollection);

myInstance.Run();

System.Console.Read();

}

}

结果

IF 条件

类名: System.Activities.Statements.If

基类: NativeActivity

文件: System.Activities.dll

类型: sealed

说明: 1. [Condition]属性的类型为[ System.Activities.InArgument<bool> ],可以接受一个结果为[bool]的表达式

2. [Then] 属性 Activity容器,如果[Condition]的表达式结果为[True],执行该容器内的[Activity]

3. [Else] 属性 Activity容器,如果[Condition]的表达式结果为[False],执行该容器内的[Activity]

1.定义In参数[myInput]

2.启动流程时传入[myInput]的值为["wxd"],

3.在流程中添加[If],[Condition]属性的值为[myInput = "wxd"]

在[Else]容器中添加[WriteLine],输出值为["false"]

在[Then]容器中添加[WriteLine],输出值为["true"]

流程

<p:Activity mc:Ignorable=""

x:Class="WorkflowConsoleApplication1.Sequence1"

xmlns="http://schemas.microsoft.com/netfx/2009/xaml/activities/design"

xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"

xmlns:p="http://schemas.microsoft.com/netfx/2009/xaml/activities"

xmlns:sad="clr-namespace:System.Activities.Debugger;assembly=System.Activities"

xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">

<x:Members>

<x:Property Name="myInput" Type="p:InArgument(x:String)" />

</x:Members>

<p:Sequence>

<p:If Condition="[myInput = &quot;wxd&quot;]">

<p:If.Else>

<p:WriteLine>["false"]</p:WriteLine>

</p:If.Else>

<p:If.Then>

<p:WriteLine>["true"]</p:WriteLine>

</p:If.Then>

</p:If>

</p:Sequence>

</p:Activity>

宿主

Dictionary<string, object> inputCollection = new Dictionary<string, object>();

inputCollection.Add("myInput", "wxd");

WorkflowInstance myInstance = new WorkflowInstance(new Sequence1(), inputCollection);

myInstance.Run();

System.Console.Read();

结果

While 条件循环

类名: System.Activities.Statements.While

基类: NativeActivity

文件: System.Activities.dll

类型: sealed

说明: 1. [Condition]属性的类型为[ System.Activities.InArgument<bool> ],可以接受一个结果为[bool]的表达式

2. [Body] 属性 Activity容器,如果[Condition]的表达式结果为[True],执行该容器内的[Activity]

3. [Variables]属性的类型为只读[System.Collections.ObjectModel.Collection<System.Activities.Variable>]

1.定义变量[myVariable],默认值为[0]

2.在流程中添加[While],[Condition]属性的值为[myVariable <= 10]

在[Body]容器中添加[Sequence]容器

在[Sequence]容器添加[WriteLine],输出值为[myVariable.ToString()]

在[Sequence]容器添加[Assign],为[myVariable =myVariable +1]

流程

<p:Activity mc:Ignorable=""

x:Class="WorkflowConsoleApplication1.Sequence1"

xmlns="http://schemas.microsoft.com/netfx/2009/xaml/activities/design"

xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"

xmlns:p="http://schemas.microsoft.com/netfx/2009/xaml/activities"

xmlns:sad="clr-namespace:System.Activities.Debugger;assembly=System.Activities"

xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">

<p:Sequence>

<p:Sequence.Variables>

<p:Variable x:TypeArguments="x:Int32" Default="[0]" Name="myVariable" />

</p:Sequence.Variables>

<p:While Condition="[myVariable &lt;= 10]">

<p:Sequence>

<p:WriteLine>[myVariable.ToString()]</p:WriteLine>

<p:Assign>

<p:Assign.To>

<p:OutArgument x:TypeArguments="x:Int32">[myVariable]</p:OutArgument>

</p:Assign.To>

<p:Assign.Value>

<p:InArgument x:TypeArguments="x:Int32">[myVariable + 1]</p:InArgument>

</p:Assign.Value>

</p:Assign>

</p:Sequence>

</p:While>

</p:Sequence>

</p:Activity>

宿主

WorkflowInstance myInstance = new WorkflowInstance(new Sequence1());

myInstance.Run();

System.Console.Read();

结果

DoWhile 条件循环

类名: System.Activities.Statements.While

基类: NativeActivity

文件: System.Activities.dll

类型: sealed

说明: 1. [Condition]属性的类型为[ System.Activities.InArgument<bool> ],可以接受一个结果为[bool]的表达式

2. [Body] 属性 Activity容器,如果[Condition]的表达式结果为[True],执行该容器内的[Activity]

3. [Variables]属性的类型为只读[System.Collections.ObjectModel.Collection<System.Activities.Variable>]

4. 与[While]不同的是无论[Condition]属性的结果是[True]或[False],[Body] 中的[Activity]都会执行一次

Switch<T> 分支选择

类名: System.Activities.Statements.Switch<T>

基类: NativeActivity

文件: System.Activities.dll

类型: sealed

说明: 

1.[Expression]属性的类型为[System.Activities.InArgument<T>],是case的 表达式

2.在添加[Switch<T>]时,需要为case 表达式 [Expression属性] 指定类型

3.[Default] 属性 是Activity容器,当 [Expression属性] 没有对应的case时,执行该容器内的[Activity]

4.[Cases]集合类型为[System.Collections.Generic.IDictionary<T, System.Activities.WorkflowElement>],是case的分支集合

1.定义变量[myVariable],默认值为[1]

2.在流程中添加[Switch<T>],类型选[Int32]

[Expression]属性的值为[myVariable]

在[Default]执行容器中添加[WriteLine],输出值为["no case"]

添加case 分支,[case]为[1],执行容器中添加[WriteLine],输出值为["case 1"]

添加case 分支,[case]为[2],执行容器中添加[WriteLine],输出值为["case 2"]

流程

<p:Activity mc:Ignorable=""

x:Class="WorkflowConsoleApplication1.Sequence1"

xmlns="http://schemas.microsoft.com/netfx/2009/xaml/activities/design"

xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"

xmlns:p="http://schemas.microsoft.com/netfx/2009/xaml/activities"

xmlns:sad="clr-namespace:System.Activities.Debugger;assembly=System.Activities"

xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">

<p:Sequence >

<p:Sequence.Variables>

<p:Variable x:TypeArguments="x:Int32" Default="[1]" Name="myVariable" />

</p:Sequence.Variables>

<p:Switch x:TypeArguments="x:Int32" Expression="[myVariable]">

<p:Switch.Default>

<p:WriteLine>["no case"]</p:WriteLine>

</p:Switch.Default>

<p:WriteLine x:Key="1">["case 1"]</p:WriteLine>

<p:WriteLine x:Key="2">["case 2"]</p:WriteLine>

</p:Switch>

</p:Sequence>

</p:Activity>

宿主

WorkflowInstance myInstance = new WorkflowInstance(new Sequence1());

myInstance.Run();

System.Console.Read();

结果

 

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