共用方式為


運算式

本主題僅適用於 Windows Workflow Foundation 4。

Windows Workflow Foundation (WF) 運算式是傳回結果的任何活動。 所有直接衍生自 Activity 的運算式活動,包含以活動傳回值命名為 ResultOutArgument 屬性。WF 隨附大範圍的運算式活動,包含如 VariableValueVariableReference 這類透過運算子活動存取單一工作流程變數的簡易活動,到如 VisualBasicReferenceVisualBasicValue 這類可存取完整 Visual Basic 語言範圍以產生結果的複雜活動。 其他運算式活動可從 CodeActivityNativeActivity 來衍生建立。

使用運算式

工作流程設計工具會針對所有運算式使用 VisualBasicValueVisualBasicReference。 這就是為何 Visual Basic 語法必須用於設計工具之運算式文字方塊的原因。 由設計工具產生的工作流程會儲存為 XAML,其中會以方括號括住運算式,如以下範例所示。

<Sequence xmlns="https://schemas.microsoft.com/netfx/2009/xaml/activities" xmlns:x="https://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))
        }
    }
};

工作流程也可以使用 Visual Basic 運算式活動表示,如以下範例所示。

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>(new VisualBasicValue<int>("a + b + c"))
        }
    }
};

使用自訂運算式活動延伸可用的運算式

.NET Framework 4 中的運算式可延伸,以便建立其他的運算式活動。 下列程式碼範例示範會傳回三個整數值加總的活動。

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)
                }
            }
        }
    }
};

如需詳細資訊以程式碼使用運算式的詳細資訊,請參閱使用命令式程式碼撰寫工作流程