最直观也可能是正确的方法是将您在流程图级别声明的变量传递到您的自定义活动中。然后你可以用它的价值做任何你想做的事情并返回它。
自定义增量活动的示例(这也是分配活动的工作方式):
public class IncrementActivity : CodeActivity<int>
{
[RequiredArgument]
public InArgument<int> CountVariable { get; set; }
protected override int Execute(CodeActivityContext context)
{
// Do whatever logic you want here
return CountVariable.Get(context) + 1;
}
}
这是使用序列的示例(使用流程图时相同):
var countVar = new Variable<int>("count");
var activity = new Sequence
{
Variables =
{
// declare counter variable at global scope
countVar
},
Activities =
{
new WriteLine { Text = new VisualBasicValue<string>(@"""Count: "" & count") },
new IncrementActivity { CountVariable = countVar, Result = countVar },
new WriteLine { Text = new VisualBasicValue<string>(@"""Count: "" & count") },
new IncrementActivity { CountVariable = countVar, Result = countVar },
new WriteLine { Text = new VisualBasicValue<string>(@"""Count: "" & count") },
new IncrementActivity { CountVariable = countVar, Result = countVar }
}
};
输出:
Count: 0
Count: 1
Count: 2
请注意,通过可视化设计器更简单,因为您不必直接使用VisualBasicValue<string>来构建打印字符串。除此之外,一模一样!