This section describes how to add multiple activities to the WF4.0 process and how to customize the Activity.
Download this example:
Http://files.cnblogs.com/foundation/SequenceSample.rar
Http://files.cnblogs.com/foundation/CodeActivitySample.rar
Directory
1. sequential container Sequence and CodeActivity 1
1.1 sequential container Sequence 1
1.2
CodeActivity 2
Sequential container Sequence and CodeActivity
Sequential container Sequence
Class Name |
System. Activities. Statements. Sequence |
File |
System. Activities. dll |
Structure Description |
Inherit NativeActivity
Is a sealed class
The [Collection <Activity> Activities attribute] and [Collection <Variable> Variables attribute] are defined.
Override [CacheMetadata method] and [Execute method] |
Function Description |
1. [Activities] set, which can store Activity
2. The Activities in the [Activities] set are executed from top to bottom in order.
3. The [Variables] set is used to store the variable |
In WF, any class derived from Activity can be instantiated and run, for example:
WriteLine myWorkfloe = new WriteLine () {Text = new InArgument <string> ("wxxinter ")};
WorkflowInvoker. Invoke (myWorkfloe ); |
|
The process created using the built-in VS2010 template is [System. activities. activity] is created as the root. In this case, only one [Activity] can be added to the design container in the designer. Of course, the process usually has multiple nodes. To solve this problem, we can add a Sequence container [Sequence] to the [Activity root] In the designer. In the Sequence container [Sequence], we can add multiple [activities], the [activity] is executed in the ascending and descending order of the [Sequence] of the sequential container during the process running.
Execution result
The following process is defined by code:
Sequence wxwinterWorkflow = new Sequence ();
WriteLine writeLine1 = new WriteLine () {Text = new InArgument <string> ("")};
WriteLine writeLine2 = new WriteLine () {Text = new InArgument <string> ("B ")};
WriteLine writeLine3 = new WriteLine () {Text = new InArgument <string> ("c ")};
WxwinterWorkflow. Activities. Add (writeLine1 );
WxwinterWorkflow. Activities. Add (writeLine2 );
WxwinterWorkflow. Activities. Add (writeLine3 );
WorkflowInvoker. Invoke (wxwinterWorkflow );
|
Execution result
|
CodeActivity
WF4.0 provides many activities, but sometimes these Activtiy functions cannot be completed, such as adding data to the database and sending emails. when implementing these specific functions, we still need to use a certain programming language, such as C #. codeActivity can be used when this function is required.
Class Name |
System. Activities. CodeActivity |
File |
System. Activities. dll |
Structure Description |
Inherit Activity
Is an abstract class
Provides [abstract Void Execute (CodeActivityContext context)] Method |
Function Description |
After a class is inherited, The Execute method of the override class is required to compile custom function code. |
The following is an example of CodeActivity, which enables you to obtain user input from the console and print the input result.
1. Create a [Workflow Console Application] WF project named [CodeActivitySample]
2. Add a [Code Activity] item named [myActivity] for [CodeActivitySample]
3. The Code content of myActivity is as follows:
Public Sealed Class MyActivity: CodeActivity
{
Protected Override Void Execute (CodeActivityContext context)
{
System. Console. Write ("Enter the following content :");
String inputString = System. Console. ReadLine ();
String outputString = string. Format ("You entered: {0}", inputString );
System. Console. WriteLine (outputString );
} |
4. After compilation, you will see [myActivity] on the toolbar and add it to the process designer:
5. Run the program. The result is as follows: