WF4.0 基础篇 (二十六) Interop调用WF3.X的Activity
本节主要介绍Interop调用WF3.X的Activity
本文例子下载:
https://files.cnblogs.com/foundation/InteropSample.rar
本文例子说明
Interop调用WF3.X的Activity
类名 | System.Activities.Statements.Interop |
文件 | System.Workflow.Runtime.dll |
结构说明 | 继承 NativeActivity, ICustomTypeDescriptor 是一个 sealed类 override 了 [CacheMetadata方法] 与 [Cancel方法] 与[Execute方法] override 了 [CanInduceIdle属性] [ActivityMetaProperties]属性的类型为[IDictionary<string, object>] [ActivityProperties]属性的类型为[IDictionary<string, object>] [ActivityType]属性的类型为[Type] |
功能说明 | [ActivityType属性]用于指定要调用的WF3.5的Activity的类型 当指定WF3.5的Activity的类型后,在设计器中会自动为WF3.5的Activity的绑定生成两个参数一个为[属性名],另一个为[属性名Out] |
使用说明
默认有WF4.0项目为[.NET Framework 4 Client Profile],要使用Interop,应将项目改为[.NET Framework 4 ]
Interop默认没出现在工具栏中,需要添加
[ActivityType属性]用于指定要调用的WF3.5的Activity的类型
当指定WF3.5的Activity的类型后,在设计器中会自动为WF3.5的Activity的绑定生成两个参数一个为[属性名],另一个为[属性名Out]
例子1:调用WF3.X的Activity
说明:
1.创建一WF3.X的Activity,有一绑定属性[printText]
Activity在[ Execute]方法中打印[printText]的值,并为[printText]赋予一新值
2.创建一WF4的工作流,用[Interop]调用WF3.X的Activity,为[printText]传入值,并打印通过[printText]的返回值
WF3.X的Activity | public partial class myActivity : Activity { public myActivity() { InitializeComponent(); }
[System.Diagnostics.DebuggerNonUserCode] [System.CodeDom.Compiler.GeneratedCode("", "")] private void InitializeComponent() { this.Name = "myActivity"; }
public static readonly DependencyProperty printTextProperty = DependencyProperty.Register("printText", typeof(string), typeof(myActivity));
public string printText { get { return (string)base.GetValue(printTextProperty); } set { base.SetValue(printTextProperty, value); } }
protected override ActivityExecutionStatus Execute(ActivityExecutionContext context) { Console.WriteLine(this.printText); this.printText = printText + ":wxd"; return ActivityExecutionStatus.Closed; } } |
WF4的工作流 | |
宿主 | static void test1() { WorkflowInvoker.Invoke(new testWorkflow());
} |
结果 |
例2:代码操作
static void test2() {
Variable<string> myVariable = new Variable<string>() { Default = "wxwinter" }; Sequence root = new Sequence() { Variables = { myVariable }, Activities = {
new Interop() { ActivityType = typeof(myActivity), ActivityProperties = {
{ "printText", new InArgument<string>(myVariable) } , { "printTextOut", new OutArgument<string>(myVariable) }
}, ActivityMetaProperties = { // Provide a value for the Name meta-property of the WriteLine { "Name", "myActivity" } } } , new WriteLine{ Text=myVariable} } };
WorkflowInvoker.Invoke(root); } |