首页 > 代码库 > 动态修改工作流<第九篇>

动态修改工作流<第九篇>

一、动态添加或移除工作流活动

  首先添加一个顺序的空白工作流。

  

  然后添加一个Winform程序,界面如下:

  

  代码如下:

namespace WinForm{    public partial class Form1 : Form    {        private WorkflowRuntime workflowRuntime = null;        private WorkflowInstance workflowInstance = null;        private WorkflowChanges wfchange = null;        public Form1()        {            InitializeComponent();            workflowRuntime = new WorkflowRuntime();            workflowRuntime.StartRuntime();        }        private void button1_Click(object sender, EventArgs e)        {            Type type = typeof(WorkflowConsoleApplication1.Workflow1);            workflowInstance = workflowRuntime.CreateWorkflow(type);            Activity Act = workflowInstance.GetWorkflowDefinition();            wfchange = new WorkflowChanges(Act);            CodeActivity code1 = new CodeActivity("codeactivity1");            code1.ExecuteCode += delegate(object s, EventArgs ea)            {                MessageBox.Show("工作流被调用!");            };            wfchange.TransientWorkflow.Activities.Insert(0, code1);        }        private void button2_Click(object sender, EventArgs e)        {            workflowInstance.ApplyWorkflowChanges(wfchange);            workflowInstance.Start();        }    }}

  效果如下:

  

二、动态添加或移除容器活动

  向工作流的容器类型复合活动中动态添加一个子活动。

  添加一个Sequence容器,然后通过代码向其内部动态添加一个Code类型的子活动。

  

  添加一个Winform程序,界面不变:

namespace WinForm{    public partial class Form1 : Form    {        private WorkflowRuntime workflowRuntime = null;        private WorkflowInstance workflowInstance = null;        private WorkflowChanges wfchange = null;        public Form1()        {            InitializeComponent();            workflowRuntime = new WorkflowRuntime();            workflowRuntime.StartRuntime();        }        //添加一个工作流        private void button1_Click(object sender, EventArgs e)        {            Type type = typeof(WorkflowConsoleApplication1.Workflow1);            workflowInstance = workflowRuntime.CreateWorkflow(type);            Activity Act = workflowInstance.GetWorkflowDefinition();            wfchange = new WorkflowChanges(Act);            //创建一个Code活动            CodeActivity code1 = new CodeActivity("codeactivity1");            code1.ExecuteCode += delegate(object s, EventArgs ea)            {                MessageBox.Show("sequenceActivity里的Code工作流被调用!");            };            //添加工作流到容器中            SequenceActivity seq = (SequenceActivity)wfchange.TransientWorkflow.Activities["sequenceActivity1"];            seq.Activities.Add(code1);        }        //移除一个工作流        private void button3_Click(object sender, EventArgs e)        {            Activity Code1 = wfchange.TransientWorkflow.Activities["codeactivity1"];            SequenceActivity seq = (SequenceActivity)wfchange.TransientWorkflow.Activities["sequenceActivity1"];            seq.Activities.RemoveAt(0);        }        //执行        private void button2_Click(object sender, EventArgs e)        {            workflowInstance.ApplyWorkflowChanges(wfchange);            workflowInstance.Start();        }    }}

  改动的部分就是红色部分代码,添加删除一个活动一样是Add、Remove。

 

动态修改工作流<第九篇>