1 vs2012中新建项目,选择.net framework3.0->workflow->顺序工作流控制台程序
2 在Workflow1.cs中,拖入IfElse和两个Code活动,并改名
3 添加代码条件,选择条件->选择代码->在Condition中输入代码方法,按回车
4选择ApprovalOrder活动,在属性事件窗口,选择ExecuteCode按回车
5 修改后的Workflow代码
using System;
using System.ComponentModel;
using System.ComponentModel.Design;
using System.Collections;
using System.Workflow.ComponentModel.Compiler;
using System.Workflow.ComponentModel.Serialization;
using System.Workflow.ComponentModel;
using System.Workflow.ComponentModel.Design;
using System.Workflow.Runtime;
using System.Workflow.Activities;
using System.Workflow.Activities.Rules;
namespace MyApprovalOrder
{
public sealed partial class Workflow1 : SequentialWorkflowActivity
{
public Workflow1()
{
InitializeComponent();
}
private bool _Approved =false;
public bool Approved
{
get { return _Approved; }
set { _Approved = value; }
}
private void IsApproved(object sender, ConditionalEventArgs e)
{
e.Result = _Approved;
}
private void ApprovalOrder_ExecuteCode(object sender, EventArgs e)
{
Console.WriteLine("订单被批准了");
}
private void RejectOrder_ExecuteCode(object sender, EventArgs e)
{
Console.WriteLine("订单被取消了");
}
}
}
6 修改Program代码
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading;
using System.Workflow.Runtime;
using System.Workflow.Runtime.Hosting;
namespace MyApprovalOrder
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("是否核准该单据?请输入Y或者Yes");
string str = Console.ReadLine();
using (WorkflowRuntime workflowRuntime = new WorkflowRuntime())
{
AutoResetEvent waitHandle = new AutoResetEvent(false);
workflowRuntime.WorkflowCompleted += delegate(object sender, WorkflowCompletedEventArgs e) { waitHandle.Set(); };
workflowRuntime.WorkflowTerminated += delegate(object sender, WorkflowTerminatedEventArgs e)
{
Console.WriteLine(e.Exception.Message);
waitHandle.Set();
};
Dictionary<string, object> wfArgument = new Dictionary<string, object>();
bool approvedArg = str == "Y" ? true : false;
wfArgument.Add("Approved", approvedArg);
WorkflowInstance instance = workflowRuntime.CreateWorkflow(typeof(MyApprovalOrder.Workflow1),wfArgument);
instance.Start();
waitHandle.WaitOne();
}
Console.Read();
}
}
}