本節主要介紹如何在WF4.0中參數與變數的使用原則與技巧,這些內容是學習WF4.0首先要掌握的
本文例子下載:
http://files.cnblogs.com/foundation/VariablesAndArgumentsSample.rar
本文例子說明
資料的傳遞 變數與參數的使用
[參數] 可以用 [運算式方式] 與 [變數]綁定
[In 參數],關聯變數值可傳入內部;內部修改參數時,關聯變數不會更改
說明:
1.定義一個[InChangeActivity],有一個string型[In 參數]myIn
在[Execute]方法中列印myIn的值並修改myIn的值後再次列印
2.在xaml工作流程中添加一個string型[myVariable]變數
3. 在xaml工作流程中添加一個[Assign],為[myVariable]變數賦值
4. 在xaml工作流程中添加[InChangeActivity], [InChangeActivity.myIn]綁定到[myVariable]變數
5. 在xaml工作流程中添加[WriteLine],列印[myVariable]變數值
InChangeActivity |
public sealed class InChangeActivity : CodeActivity { public InArgument<string> myIn { get; set; } protected override void Execute(CodeActivityContext context) { string s1 = context.GetValue(this.myIn); System.Console.WriteLine("傳入值為a:{0}", s1); // context.SetValue(myIn, Guid.NewGuid().ToString()); // string s2 = context.GetValue(this.myIn); System.Console.WriteLine("內部修改:{0}", s2); } } |
工作流程 |
|
宿主 |
WorkflowInvoker.Invoke(new InChangeWorkflow()); |
結果 |
|
[Out 參數] 關聯變數值無法傳入內部,內部參數修改時,會更新其關聯的變數
說明:
1.定義一個[OutChangeActivity],有一個string型[Out 參數]myOut
在[Execute]方法中列印myOut的值並修改myOut的值後再次列印
2.在xaml工作流程中添加一個string型[myVariable]變數
3. 在xaml工作流程中添加一個[Assign],為[myVariable]變數賦值
4. 在xaml工作流程中添加[OutChangeActivity], [OutChangeActivity.myOut]綁定到[myVariable]變數
5. 在xaml工作流程中添加[WriteLine],列印[myVariable]變數值
OutChangeActivity |
public sealed class OutChangeActivity : CodeActivity { public OutArgument<string> myOut { get; set; } protected override void Execute(CodeActivityContext context) { string s1 = context.GetValue(this.myOut); System.Console.WriteLine("傳入值為:{0}", s1); // context.SetValue(myOut, Guid.NewGuid().ToString()); // string s2 = context.GetValue(this.myOut); System.Console.WriteLine("內部修改:{0}", s2); } } |
工作流程 |
|
宿主 |
WorkflowInvoker.Invoke(new OutChangeWorkflow()); |
結果 |
|
[In/Out 參數]關聯變數值可傳入內部;內部參數修改時,會更新其關聯的變數
說明:
1.定義一個 [InOutChangeActivity],有一個string型[InOut 參數]myInOut
在[Execute]方法中列印myInOut的值並修改myInOut的值後再次列印
2.在xaml工作流程中添加一個string型[myVariable]變數
3. 在xaml工作流程中添加一個[Assign],為[myVariable]變數賦值
4. 在xaml工作流程中添加[InOutChangeActivity], [InOutChangeActivity.myInOut]綁定到[myVariable]變數
5. 在xaml工作流程中添加[WriteLine],列印[myVariable]變數值
InOutChangeActivity |
public sealed class InOutChangeActivity : CodeActivity { public InOutArgument<string> myInOut { get; set; } protected override void Execute(CodeActivityContext context) { string s1 = context.GetValue(this.myInOut); System.Console.WriteLine("傳入值為a:{0}", s1); // context.SetValue(myInOut, Guid.NewGuid().ToString()); // string s2 = context.GetValue(this.myInOut); System.Console.WriteLine("內部修改:{0}", s2); } } |
工作流程 |
|
宿主 |
WorkflowInvoker.Invoke(new InOutChangeWorkflow()); |
結果 |
|