一、动态添加或移除活动
首先添加一个顺序的空白工作流。
然后添加一个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 button3_Click(object sender, EventArgs e) { Activity Code1 = wfchange.TransientWorkflow.Activities["codeactivity1"]; wfchange.TransientWorkflow.Activities.Remove(Code1); } //执行 private void button2_Click(object sender, EventArgs e) { workflowInstance.ApplyWorkflowChanges(wfchange); workflowInstance.Start(); } } }
效果如下:
点击"添加Code"后,再执行,Code1活动会被执行。
如果点击"添加Code"再点击"移除Code",那么不会弹出任何提示框。
时间: 2024-10-12 18:07:26