WF 中的集合活动

本主题适用于 Windows Workflow Foundation 4。

集合活动用于使用工作流中的集合对象。.NET Framework 版本 4包含多个系统提供的活动,用于在集合中添加和移除项、测试集合中是否存在某个项以及清除集合。所有集合活动都是继承自 CodeActivityCodeActivity 的泛型类;ExistsInCollectionRemoveFromCollection 具有一个类型为 BooleanOutArgument,用于指示结果。

Ee358729.Important(zh-cn,VS.100).gif 注意:
如果在设置基础集合对象之前执行集合活动,则会引发 InvalidOperationException,并且活动将发生错误。

集合活动

AddToCollection

向指定集合中添加项。

ClearCollection

清除指定集合中的所有项。

ExistsInCollection

如果集合中存在某个项,则返回 true

RemoveFromCollection

从指定集合中移除某个项,并在成功移除后返回 true

使用集合活动

下面的代码示例演示如何与声明为工作流变量的集合交互。使用的集合是 String 对象的 List’1,该集合命名为 fruitList

Variable<ICollection<string>> fruitList = new Variable<ICollection<string>>
{
Default = new List<string>
      {
          "Apple",
          "Orange",
      },
      Name = "FruitList",
};

Variable<bool> result = new Variable<bool>
{
      Name = "Result",
}

Sequence seq1 = 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="https://schemas.microsoft.com/netfx/2009/xaml/activities" xmlns:scg="clr-namespace:System.Collections.Generic;assembly=mscorlib" xmlns:x="https://schemas.microsoft.com/winfx/2006/xaml">
  <Sequence.Variables>
    <Variable x:TypeArguments="scg:ICollection(x:String)" Name="FruitList">
      <Variable.Default>
        <Literal x:TypeArguments="scg:ICollection(x:String)" Result="{x:Null}">
          <scg:List x:TypeArguments="x:String" Capacity="4">
            <x:String>Orange</x:String>
          </scg:List>
        </Literal>
      </Variable.Default>
    </Variable>
    <Variable x:TypeArguments="x:Boolean" Name="Result" />
  </Sequence.Variables>
  <If>
    <If.Condition>
      <InArgument x:TypeArguments="x:Boolean">
        <ExistsInCollection x:TypeArguments="x:String" Item="Pear" Result="{x:Null}">[FruitList]</ExistsInCollection>
      </InArgument>
    </If.Condition>
    <If.Else>
     <RemoveFromCollection x:TypeArguments="x:String" Item="Apple" Result="{x:Null}">[FruitList]</RemoveFromCollection>
    </If.Else>
    <If.Then>
      <AddToCollection x:TypeArguments="x:String" Item="Pear">[FruitList]</AddToCollection>
    </If.Then>
  </If>
  <RemoveFromCollection x:TypeArguments="x:String" Item="Apple" Result="[Result]">[FruitList]</RemoveFromCollection>
  <If Condition="[Result]">
    <If.Then>
      <ClearCollection x:TypeArguments="x:String">[FruitList]</ClearCollection>
    </If.Then>
  </If>
</Sequence>