WF 中的集合活动
集合活动用于使用工作流中的集合对象。 .NET Framework 4.6.1 包含多个系统提供的活动,用于在集合中添加和移除项、测试集合中是否存在某个项以及清除集合。 ExistsInCollection
和 RemoveFromCollection
有一个 Boolean 类型的 OutArgument<T>,指示结果。
重要
如果在设置基础集合对象之前执行集合活动,则会引发 InvalidOperationException,并且活动将发生错误。
集合活动
活动类型 | 说明 |
---|---|
AddToCollection<T> | 向指定集合中添加项。 |
ClearCollection<T> | 清除指定集合中的所有项。 |
ExistsInCollection<T> | 如果集合中存在某个项,则返回 true 。 |
RemoveFromCollection<T> | 从指定集合中移除某个项,并在成功移除后返回 true 。 |
使用集合活动
下面的代码示例演示如何与声明为工作流变量的集合交互。 使用的集合是 List<T> 对象的 String,该集合命名为 fruitList
。
Variable<ICollection<string>> fruitList = new Variable<ICollection<string>>
{
Default = new VisualBasicValue<ICollection<string>>("New List(Of String) From {\"Apple\", \"Orange\"}"),
Name = "FruitList"
};
Variable<bool> result = new Variable<bool>
{
Name = "Result"
};
Activity wf = new Sequence
{
Variables = { fruitList, result },
Activities =
{
new If
{
Condition = new ExistsInCollection<string>
{
Collection = fruitList,
Item = "Pear"
},
Then = new AddToCollection<string>
{
Collection = fruitList,
Item = "Pear"
},
Else = new RemoveFromCollection<string>
{
Collection = fruitList,
Item = "Apple"
}
},
new RemoveFromCollection<string>
{
Collection = fruitList,
Item = "Apple",
Result = result
},
new If
{
Condition = result,
Then = new ClearCollection<string>
{
Collection = fruitList,
}
}
}
};
<Sequence
xmlns="http://schemas.microsoft.com/netfx/2009/xaml/activities"
xmlns:scg="clr-namespace:System.Collections.Generic;assembly=mscorlib"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<Sequence.Variables>
<x:Reference>__ReferenceID0</x:Reference>
<x:Reference>__ReferenceID1</x:Reference>
</Sequence.Variables>
<If>
<If.Condition>
<InArgument
x:TypeArguments="x:Boolean">
<ExistsInCollection
x:TypeArguments="x:String"
Item="Pear">
<ExistsInCollection.Result>
<OutArgument
x:TypeArguments="x:Boolean" />
</ExistsInCollection.Result>
<InArgument
x:TypeArguments="scg:ICollection(x:String)">
<VariableValue
x:TypeArguments="scg:ICollection(x:String)">
<VariableValue.Result>
<OutArgument
x:TypeArguments="scg:ICollection(x:String)" />
</VariableValue.Result>
<VariableValue.Variable>
<Variable
x:TypeArguments="scg:ICollection(x:String)"
x:Name="__ReferenceID0"
Default="[New List(Of String) From {"Apple", "Orange"}]"
Name="FruitList" />
</VariableValue.Variable>
</VariableValue>
</InArgument>
</ExistsInCollection>
</InArgument>
</If.Condition>
<If.Then>
<AddToCollection
x:TypeArguments="x:String"
Item="Pear">
<InArgument
x:TypeArguments="scg:ICollection(x:String)">
<VariableValue
x:TypeArguments="scg:ICollection(x:String)"
Variable="{x:Reference __ReferenceID0}">
<VariableValue.Result>
<OutArgument
x:TypeArguments="scg:ICollection(x:String)" />
</VariableValue.Result>
</VariableValue>
</InArgument>
</AddToCollection>
</If.Then>
<If.Else>
<RemoveFromCollection
x:TypeArguments="x:String"
Item="Apple"
Result="{x:Null}">
<InArgument
x:TypeArguments="scg:ICollection(x:String)">
<VariableValue
x:TypeArguments="scg:ICollection(x:String)"
Variable="{x:Reference __ReferenceID0}">
<VariableValue.Result>
<OutArgument
x:TypeArguments="scg:ICollection(x:String)" />
</VariableValue.Result>
</VariableValue>
</InArgument>
</RemoveFromCollection>
</If.Else>
</If>
<RemoveFromCollection
x:TypeArguments="x:String"
Item="Apple">
<RemoveFromCollection.Result>
<OutArgument
x:TypeArguments="x:Boolean">
<VariableReference
x:TypeArguments="x:Boolean">
<VariableReference.Result>
<OutArgument
x:TypeArguments="Location(x:Boolean)" />
</VariableReference.Result>
<VariableReference.Variable>
<Variable
x:TypeArguments="x:Boolean"
x:Name="__ReferenceID1"
Name="Result" />
</VariableReference.Variable>
</VariableReference>
</OutArgument>
</RemoveFromCollection.Result>
<InArgument
x:TypeArguments="scg:ICollection(x:String)">
<VariableValue
x:TypeArguments="scg:ICollection(x:String)"
Variable="{x:Reference __ReferenceID0}">
<VariableValue.Result>
<OutArgument
x:TypeArguments="scg:ICollection(x:String)" />
</VariableValue.Result>
</VariableValue>
</InArgument>
</RemoveFromCollection>
<If>
<If.Condition>
<InArgument
x:TypeArguments="x:Boolean">
<VariableValue
x:TypeArguments="x:Boolean"
Variable="{x:Reference __ReferenceID1}">
<VariableValue.Result>
<OutArgument
x:TypeArguments="x:Boolean" />
</VariableValue.Result>
</VariableValue>
</InArgument>
</If.Condition>
<If.Then>
<ClearCollection
x:TypeArguments="x:String">
<InArgument
x:TypeArguments="scg:ICollection(x:String)">
<VariableValue
x:TypeArguments="scg:ICollection(x:String)"
Variable="{x:Reference __ReferenceID0}">
<VariableValue.Result>
<OutArgument
x:TypeArguments="scg:ICollection(x:String)" />
</VariableValue.Result>
</VariableValue>
</InArgument>
</ClearCollection>
</If.Then>
</If>
</Sequence>
还可以使用 CSharpValue<TResult>(而不是 VisualBasicValue<TResult>)创建上面的代码示例。
Variable<ICollection<string>> fruitList = new Variable<ICollection<string>>
{
Default = new CSharpValue<ICollection<string>>("new List<String> From {\"Apple\", \"Orange\"};"),
Name = "FruitList"
};
Variable<bool> result = new Variable<bool>
{
Name = "Result"
};
Activity wf = new Sequence
{
Variables = { fruitList, result },
Activities =
{
new If
{
Condition = new ExistsInCollection<string>
{
Collection = fruitList,
Item = "Pear"
},
Then = new AddToCollection<string>
{
Collection = fruitList,
Item = "Pear"
},
Else = new RemoveFromCollection<string>
{
Collection = fruitList,
Item = "Apple"
}
},
new RemoveFromCollection<string>
{
Collection = fruitList,
Item = "Apple",
Result = result
},
new If
{
Condition = result,
Then = new ClearCollection<string>
{
Collection = fruitList,
}
}
}
};
<Sequence
xmlns="http://schemas.microsoft.com/netfx/2009/xaml/activities"
xmlns:scg="clr-namespace:System.Collections.Generic;assembly=mscorlib"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<Sequence.Variables>
<x:Reference>__ReferenceID0</x:Reference>
<x:Reference>__ReferenceID1</x:Reference>
</Sequence.Variables>
<If>
<If.Condition>
<InArgument
x:TypeArguments="x:Boolean">
<ExistsInCollection
x:TypeArguments="x:String"
Item="Pear">
<ExistsInCollection.Result>
<OutArgument
x:TypeArguments="x:Boolean" />
</ExistsInCollection.Result>
<InArgument
x:TypeArguments="scg:ICollection(x:String)">
<VariableValue
x:TypeArguments="scg:ICollection(x:String)">
<VariableValue.Result>
<OutArgument
x:TypeArguments="scg:ICollection(x:String)" />
</VariableValue.Result>
<VariableValue.Variable>
<Variable
x:TypeArguments="scg:ICollection(x:String)"
x:Name="__ReferenceID0"
Default="[new List<String> From {"Apple", "Orange"};]"
Name="FruitList" />
</VariableValue.Variable>
</VariableValue>
</InArgument>
</ExistsInCollection>
</InArgument>
</If.Condition>
<If.Then>
<AddToCollection
x:TypeArguments="x:String"
Item="Pear">
<InArgument
x:TypeArguments="scg:ICollection(x:String)">
<VariableValue
x:TypeArguments="scg:ICollection(x:String)"
Variable="{x:Reference __ReferenceID0}">
<VariableValue.Result>
<OutArgument
x:TypeArguments="scg:ICollection(x:String)" />
</VariableValue.Result>
</VariableValue>
</InArgument>
</AddToCollection>
</If.Then>
<If.Else>
<RemoveFromCollection
x:TypeArguments="x:String"
Item="Apple"
Result="{x:Null}">
<InArgument
x:TypeArguments="scg:ICollection(x:String)">
<VariableValue
x:TypeArguments="scg:ICollection(x:String)"
Variable="{x:Reference __ReferenceID0}">
<VariableValue.Result>
<OutArgument
x:TypeArguments="scg:ICollection(x:String)" />
</VariableValue.Result>
</VariableValue>
</InArgument>
</RemoveFromCollection>
</If.Else>
</If>
<RemoveFromCollection
x:TypeArguments="x:String"
Item="Apple">
<RemoveFromCollection.Result>
<OutArgument
x:TypeArguments="x:Boolean">
<VariableReference
x:TypeArguments="x:Boolean">
<VariableReference.Result>
<OutArgument
x:TypeArguments="Location(x:Boolean)" />
</VariableReference.Result>
<VariableReference.Variable>
<Variable
x:TypeArguments="x:Boolean"
x:Name="__ReferenceID1"
Name="Result" />
</VariableReference.Variable>
</VariableReference>
</OutArgument>
</RemoveFromCollection.Result>
<InArgument
x:TypeArguments="scg:ICollection(x:String)">
<VariableValue
x:TypeArguments="scg:ICollection(x:String)"
Variable="{x:Reference __ReferenceID0}">
<VariableValue.Result>
<OutArgument
x:TypeArguments="scg:ICollection(x:String)" />
</VariableValue.Result>
</VariableValue>
</InArgument>
</RemoveFromCollection>
<If>
<If.Condition>
<InArgument
x:TypeArguments="x:Boolean">
<VariableValue
x:TypeArguments="x:Boolean"
Variable="{x:Reference __ReferenceID1}">
<VariableValue.Result>
<OutArgument
x:TypeArguments="x:Boolean" />
</VariableValue.Result>
</VariableValue>
</InArgument>
</If.Condition>
<If.Then>
<ClearCollection
x:TypeArguments="x:String">
<InArgument
x:TypeArguments="scg:ICollection(x:String)">
<VariableValue
x:TypeArguments="scg:ICollection(x:String)"
Variable="{x:Reference __ReferenceID0}">
<VariableValue.Result>
<OutArgument
x:TypeArguments="scg:ICollection(x:String)" />
</VariableValue.Result>
</VariableValue>
</InArgument>
</ClearCollection>
</If.Then>
</If>
</Sequence>