WF4.0 应用篇(二) 表达式
Literal <T>表达式
类名:System.Activities.Expressions.Literal<T>
继承:System.Activities.CodeActivity<TResult>
接口:System.Activities.XamlIntegration.IValueSerializableExpression
Dll:System.Activities.dll
返回一个值的Activity
除了String,使用引用类型的实例初始化Literal<T> 是无效的
static void test1() { Literal<String> obj = new Literal<string>() { Value = "wxwinter" };
var v = WorkflowInvoker.Invoke(obj);
System.Console.WriteLine(v); } |
在WF的参数中使用Literal<T>
在WF设计器上,表达式使用方括号序列化。这表示是 VisualBasicValue<T> 或VisualBasicReference<T>。例如,在设计器上放一个WriteLine,设置它的Text为表达式:(100+200).ToString 其XAML为<WriteLine Text="[(100 + 200).ToString]" />
并不是所有的ExpressionTextBox的值都被序列化成这种格式。
Primitives, strings, TimeSpan, 和DateTime被序列化成字面值。例如,将WriteLine 的Text改成:"Wxwinter" , 其XAML为<WriteLine Text="Wxwinter" />
这时,WF将"Wxwinter" 封装到 Literal<String>中执行。
static void test2() {
{ WriteLine wr = new WriteLine();
wr.Text = "wxwinter";
WorkflowInvoker.Invoke(wr); } //上面的方式与下面的方式相同 { WriteLine wr = new WriteLine();
wr.Text = new Literal<string>() { Value = "wxwinter" };
WorkflowInvoker.Invoke(wr); }
} |
VisualBasicValue<TResult>
类名:Microsoft.VisualBasic.Activities.VisualBasicValue<TResult>
继承:System.Activities.CodeActivity<TResult>
接口:System.Activities.XamlIntegration.IValueSerializableExpression
Dll:System.Activities.dll
可以返回将字符串表达式的运行结果
static void test3() {
VisualBasicValue<double> a = new VisualBasicValue<double>(); a.ExpressionText = "(1+2)*10 /2";
var v = WorkflowInvoker.Invoke(a); System.Console.WriteLine(v);
}
|
[In参数]绑定到动态参数
Activity | public sealed class myActivityA : CodeActivity {
public InArgument<string> myText { get; set; }
protected override void CacheMetadata(CodeActivityMetadata metadata) { myText = new VisualBasicValue<string> { ExpressionText = "myIn" }; base.CacheMetadata(metadata); }
protected override void Execute(CodeActivityContext context) {
string text = context.GetValue(this.myText);
System.Console.WriteLine("myActivityA:" + text); } } |
流程 | |
宿主 | static void test4() { var wf = new workflowA { myIn = "wxwinter" }; WorkflowInvoker.Invoke(wf); } |
结果 |
[In参数]绑定到变量
Activity | public sealed class myActivityB : CodeActivity { public InArgument<string> myText { get; set; }
protected override void CacheMetadata(CodeActivityMetadata metadata) { myText = new VisualBasicValue<string> { ExpressionText = "myVariable" }; base.CacheMetadata(metadata); }
protected override void Execute(CodeActivityContext context) {
string text = context.GetValue(this.myText);
System.Console.WriteLine("myActivityB:" + text); } } |
流程 | |
宿主 | static void test5() { var wf = new workflowB (); WorkflowInvoker.Invoke(wf); } |
结果 |
VisualBasicReference<TResult>
类名:Microsoft.VisualBasic.Activities.VisualBasicReference<TResult>
继承:System.Activities.CodeActivity<TResult>
接口:System.Activities.XamlIntegration.IValueSerializableExpression
Dll:System.Activities.dll
[Out参数]绑定到动态参数
Activity | public sealed class myActivityC : CodeActivity { public OutArgument<string> returnValue { get; set; }
protected override void CacheMetadata(CodeActivityMetadata metadata) { returnValue = new VisualBasicReference<string> { ExpressionText = "myOut" };
base.CacheMetadata(metadata); }
protected override void Execute(CodeActivityContext context) { returnValue.Set(context, "hello wxwinter");
} } |
流程 | |
宿主 | static void test6() { var wf = new workflowC(); WorkflowInvoker.Invoke(wf); } |
结果 |
表达式无法绑定到静态参数