WF4.0 基礎篇 (七) 資料的傳遞 變數與參數的使用

來源:互聯網
上載者:User

本節主要介紹如何在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());

結果

 

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.