Note
Access to this page requires authorization. You can try signing in or changing directories.
Access to this page requires authorization. You can try changing directories.
A Windows Workflow Foundation (WF) expression is any activity that returns a result. All expression activities derive indirectly from Activity
Using Expressions
Workflow designer uses VisualBasicValue
Note
Support for C# expressions in workflow projects was introduced in .NET Framework 4.5. For more information, see C# Expressions.
Workflows produced by designer are saved in XAML, where expressions appear enclosed in square brackets, as in the following example.
[r]
[a + b + c]
When defining a workflow in code, any expression activities can be used. The following example shows the usage of a composition of operator activities to add three numbers:
Variable a = new Variable("a", 1);
Variable b = new Variable("b", 2);
Variable c = new Variable("c", 3);
Variable r = new Variable("r", 0);
Sequence w = new Sequence
{
Variables = { a, b, c, r },
Activities =
{
new Assign {
To = new OutArgument(r),
Value = new InArgument {
Expression = new Add {
Left = new Add {
Left = new InArgument(a),
Right = new InArgument(b)
},
Right = new InArgument(c)
}
}
}
}
};
The same workflow can be expressed more compactly by using C# lambda expressions, as shown in the following example:
Variable a = new Variable("a", 1);
Variable b = new Variable("b", 2);
Variable c = new Variable("c", 3);
Variable r = new Variable("r", 0);
Sequence w = new Sequence
{
Variables = { a, b, c, r },
Activities =
{
new Assign {
To = new OutArgument(r),
Value = new InArgument((ctx) => a.Get(ctx) + b.Get(ctx) + c.Get(ctx))
}
}
};
Extending Available Expressions with Custom Expression Activities
Expressions in .NET Framework 4.6.1 are extensible allowing for additional expression activities to be created. The following example shows an activity that returns a sum of three integer values.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Activities;
namespace ExpressionsDemo
{
public sealed class AddThreeValues : CodeActivity
{
public InArgument Value1 { get; set; }
public InArgument Value2 { get; set; }
public InArgument Value3 { get; set; }
protected override int Execute(CodeActivityContext context)
{
return Value1.Get(context) +
Value2.Get(context) +
Value3.Get(context);
}
}
}
With this new activity you can rewrite the previous workflow that added three values as shown in the following example:
Variable a = new Variable("a", 1);
Variable b = new Variable("b", 2);
Variable c = new Variable("c", 3);
Variable r = new Variable("r", 0);
Sequence w = new Sequence
{
Variables = { a, b, c, r },
Activities =
{
new Assign {
To = new OutArgument(r),
Value = new InArgument {
Expression = new AddThreeValues() {
Value1 = new InArgument(a),
Value2 = new InArgument(b),
Value3 = new InArgument(c)
}
}
}
}
};
For more information about using expressions in code, see Authoring Workflows, Activities, and Expressions Using Imperative Code.