只有具备[Variables]属性的Activity才可以定义变量
[Variables] 的数据类型为System.Collections.ObjectModel.Collection<System.Activities.Variable>
变量添加
在WF设计器中添加
变量存放在所在[Activity]的内部,
<p:Activity mc:Ignorable="" x:Class="WorkflowConsoleApplication3.Sequence1" xmlns="http://schemas.microsoft.com/netfx/2009/xaml/activities/design" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:p="http://schemas.microsoft.com/netfx/2009/xaml/activities" xmlns:sad="clr-namespace:System.Activities.Debugger;assembly=System.Activities" xmlns:w="clr-namespace:WorkflowConsoleApplication3;assembly=WorkflowConsoleApplication3" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"> <p:Sequence > <w:myElement> <w:myElement.Variables> <p:Variable x:TypeArguments="x:String" Default="["i am wxd"]" Name="wxd" /> </w:myElement.Variables> </w:myElement> </p:Activity> |
在定义变量的[Activity]内部的[Activity]可以访问变量,在定义变量的[Activity]外部的[Activity]无法访问变量
无法通过启动传参方式为[变量]赋值.无法在流程完成事件中接收[变量]的值
定义具有[Variables]属性的Activity
自定义Activity | public class myNative :NativeActivity {
private Collection<Variable> variables; private Variable<int> lastIndexHint; public myNative() { this.variables = new WorkflowElement.DesignTimeAwareCollection<Variable>(this); this.lastIndexHint = new Variable<int> { Modifiers = VariableModifiers.SuppressTracking }; }
protected override void OnGetEnvironmentVariables(IList<Variable> variables) { for (int i = 0; i < this.Variables.Count; i++) { variables.Add(this.Variables[i]); } }
protected override void OnGetPrivateVariables(IList<Variable> variables) { variables.Add(this.lastIndexHint); }
public Collection<Variable> Variables { get { return this.variables; } }
protected override void Execute(ActivityExecutionContext context) { foreach (var v in variables) { System.Console.WriteLine("{0}:{1}",v.Name,v.Get(context));
} } } |
流程 | |
宿主 |
WorkflowInstance myInstance = new WorkflowInstance(new Sequence1());
myInstance.Run();
System.Console.Read(); |
结果 |