Next, let's take a look at how to create a workflow flow in the form of code to achieve the same goal as the designer.
1. Create a winform console application.
2. Add the Assembly reference "system. Activities ":
3. Add the following code to the main function:
4. Modify the "createworkflow method" above ":
5. Run the above program and view the running result.
Next let's build an activity with if control flow in this code to see how it is built.
1. On the basis of the above, we will modify the above "content of the createworkflow method ":
Private Static system. Activities. Activity createworkflow ()
{
// Create a new workflow Activity
Variable <datetime> nows = new variable <datetime> ()
{
Name = "datetime ",
Default = datetime. Now
};
Return new sequence ()
{
Displayname = "test ",
Activities =
{
New writeline ()
{
Displayname = "write1 ",
TEXT = "Hello world! "
}
}
};
}
The above is the simple output node of the previous output helloword without complex conditions. We will modify the above Code as follows:
First, define a variable "contion1" condition variable within the range of the sequential control flow as the if condition judgment.
Variable <int> contion1 = new variable <int> ()
{
Name = "int1 ",
Default = 1
};
Returns an ordered control flow.
Return new sequence ()
{
Variables =
{
Contion1
},
Displayname = "test ",
Activities =
{
// Output the workflow execution start date
New writeline ()
{
Displayname = "write1 ",
TEXT = system. datetime. Now. tostring ()
},
New if ()
{
Displayname = "Test2", condition = expressionservices. Convert <bool> (ENV => contion1.get (ENV)> 0 ),
Then = new writeline (){
Displayname = "then ",
TEXT = "then"
}, Else = new writeline (){
Displayname = "else ",
TEXT = "else"
}
},
// Output the workflow execution completion date
New writeline ()
{
Displayname = "write2 ",
TEXT = system. datetime. Now. tostring ()
}
}
};
2. Let's explain the preceding definition description:
3. Other content:
4. view the following results after running:
5. The statement in the IF condition and the condition parsing are described below :,
Based on the above form, we can write all the code we have completed based on the designer. Of course, we have mastered this form of console. When we combine some relevant development platforms or business code,
It is more convenient and easy. It may be difficult to write in the console.
For example, we complete the value assignment operation in then based on the above.
We assign the variable contion1 to 2 in then.
And print it out.
1. modify the code in the above sequece:
The above completes the modification of the internal process of the IF activity.
2. Check the running effect after running: