本文例子說明
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);
}
示例代碼:http://www.bianceng.net/dotnet/201208/243.htm