Compartir a través de


Actividades de colección en WF

Este tema es aplicable a Windows Workflow Foundation 4.

Las actividades de colección se usan para trabajar con objetos de colección en un flujo de trabajo. .NET Framework versión 4 tiene actividades proporcionadas por el sistema para agregar y quitar elementos de una colección, probar la existencia de un elemento en una colección y borrar una. Todas las actividades de colección son clases genéricas que se heredan de CodeActivity o CodeActivity; ExistsInCollection y RemoveFromCollection tienen un OutArgument de tipo Boolean que indica el resultado.

Ee358729.Important(es-es,VS.100).gif Nota:
Si se ejecuta una actividad de colección antes de establecer el objeto de colección subyacente, se inicia una excepción InvalidOperationException y se produce un error en la actividad.

Actividades de colección

AddToCollection

Agrega un elemento a una colección especificada.

ClearCollection

Borra todos los elementos de una colección especificada.

ExistsInCollection

Devuelve true si un elemento ya existe en una colección.

RemoveFromCollection

Quita un elemento de una colección especificada y devuelve true si el elemento se quitó correctamente.

Usar actividades de colección

El siguiente ejemplo de código muestra cómo interactuar con un colección declarada como una variable de flujo de trabajo. La colección usada es una List’1 de objetos String denominada 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>