共用方式為


表達式 (WF)

Windows Workflow Foundation(WF)中的表示式是任何能傳回結果的活動。 所有表達式活動都間接衍生自 Activity<TResult>,其中包含 OutArgument 名為 Result 的屬性做為活動的傳回值。 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>

備註

在 .NET Framework 4.5 中引進了工作流程專案中的 C# 表達式支援。 如需詳細資訊,請參閱 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)
                }
            }
        }
    }
};

如需在程式代碼中使用表達式的詳細資訊,請參閱 使用命令式程式碼撰寫工作流程、活動和表示式