表达式 (WF)

Windows Workflow Foundation (WF) 表达式是任何返回结果的活动。 从 Activity<TResult> 间接派生的所有表达式活动,其中包含名为 OutArgumentResult 属性作为活动的返回值。 WF 随附了从简单活动(如 VariableValue<T>VariableReference<T>,它们通过运算符活动提供对单个工作流变量的访问)到复杂活动(如 VisualBasicReference<TResult>VisualBasicValue<TResult>,它们提供对 Visual Basic 语言的全面访问,以获得结果)的广泛表达式活动。 可以通过从 CodeActivity<TResult>NativeActivity<TResult> 派生来创建其他表达式活动。

使用表达式

工作流设计器对 Visual Basic 项目中的所有表达式使用 VisualBasicValue<TResult>VisualBasicReference<TResult>,对 C# 工作流项目中的所有表达式使用 CSharpValue<TResult>CSharpReference<TResult>

注意

对工作流项目中 C# 表达式的支持是在 .NET Framework 4.5 中引入的。 有关详细信息,请参阅 C# 表达式

设计器生成的工作流保存在 XAML 中,其中表达式位于方括号中,如以下示例所示。

<Sequence xmlns="http://schemas.microsoft.com/netfx/2009/xaml/activities" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
  <Sequence.Variables>
    <Variable x:TypeArguments="x:Int32" Default="1" Name="a" />
    <Variable x:TypeArguments="x:Int32" Default="2" Name="b" />
    <Variable x:TypeArguments="x:Int32" Default="3" Name="c" />
    <Variable x:TypeArguments="x:Int32" Default="0" Name="r" />
  </Sequence.Variables>
  <Assign>
    <Assign.To>
      <OutArgument x:TypeArguments="x:Int32">[r]</OutArgument>
    </Assign.To>
    <Assign.Value>
      <InArgument x:TypeArguments="x:Int32">[a + b + c]</InArgument>
    </Assign.Value>
  </Assign>
</Sequence>

在代码中定义工作流时,可以使用所有表达式活动。 下面的示例演示如何使用运算符活动的组合添加三个数字:

Variable<int> a = new Variable<int>("a", 1);
Variable<int> b = new Variable<int>("b", 2);
Variable<int> c = new Variable<int>("c", 3);
Variable<int> r = new Variable<int>("r", 0);

Sequence w = new Sequence
{
    Variables = { a, b, c, r },
    Activities =
    {
        new Assign {
            To = new OutArgument<int>(r),
            Value = new InArgument<int> {
                Expression = new Add<int, int, int> {
                    Left = new Add<int, int, int> {
                        Left = new InArgument<int>(a),
                        Right = new InArgument<int>(b)
                    },
                    Right = new InArgument<int>(c)
                }
            }
        }
    }
};

使用 C# lambda 表达式可以更简洁地表示同一工作流,如下面的示例所示:

Variable<int> a = new Variable<int>("a", 1);
Variable<int> b = new Variable<int>("b", 2);
Variable<int> c = new Variable<int>("c", 3);
Variable<int> r = new Variable<int>("r", 0);

Sequence w = new Sequence
{
    Variables = { a, b, c, r },
    Activities =
    {
        new Assign {
            To = new OutArgument<int>(r),
            Value = new InArgument<int>((ctx) => a.Get(ctx) + b.Get(ctx) + c.Get(ctx))
        }
    }
};

使用自定义表达式活动扩展可用的表达式

可以扩展 .NET Framework 4.6.1 中的表达式,从而允许创建其他表达式活动。 下面的示例演示返回三个整数值之和的活动。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Activities;

namespace ExpressionsDemo
{
    public sealed class AddThreeValues : CodeActivity<int>
    {
        public InArgument<int> Value1 { get; set; }
        public InArgument<int> Value2 { get; set; }
        public InArgument<int> Value3 { get; set; }

        protected override int Execute(CodeActivityContext context)
        {
            return Value1.Get(context) +
                   Value2.Get(context) +
                   Value3.Get(context);
        }
    }
}

通过这个新活动,可以重新编写前面添加三个值的工作流,如下面的示例所示:

Variable<int> a = new Variable<int>("a", 1);
Variable<int> b = new Variable<int>("b", 2);
Variable<int> c = new Variable<int>("c", 3);
Variable<int> r = new Variable<int>("r", 0);

Sequence w = new Sequence
{
    Variables = { a, b, c, r },
    Activities =
    {
        new Assign {
            To = new OutArgument<int>(r),
            Value = new InArgument<int> {
                Expression = new AddThreeValues() {
                    Value1 = new InArgument<int>(a),
                    Value2 = new InArgument<int>(b),
                    Value3 = new InArgument<int>(c)
                }
            }
        }
    }
};

有关在代码中使用表达式的详细信息,请参阅使用命令式代码编写工作流、活动和表达式