新建一个项目->顺序工作流库
设计面板拖放ifElse活动
点右键,添加三个分支
每个分支的条件设置为声明性规则条件
并设置他们的名称和表达式
代码如下:
using System; using System.ComponentModel; using System.ComponentModel.Design; using System.Collections; using System.Drawing; using System.Linq; 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 WorkflowLibrary1 { public sealed partial class Workflow1: SequentialWorkflowActivity { public string Operation { get; set; } public int Number1 { get; set; } public int Number2 { get; set; } public double Result { get; set; } public Workflow1() { InitializeComponent(); } private void codeActivity1_ExecuteCode(object sender, EventArgs e) { Result = Number1 + Number2; } private void codeActivity2_ExecuteCode(object sender, EventArgs e) { Result = Number1 - Number2; } private void codeActivity3_ExecuteCode(object sender, EventArgs e) { Result = Number1 * Number2; } private void codeActivity4_ExecuteCode(object sender, EventArgs e) { if (Number2 != 0) { Result = (double)Number1 / (double)Number2; } else { throw new ArgumentException("除数为0"); } } private void codeActivity5_ExecuteCode(object sender, EventArgs e) { throw new ArgumentException("没有找到合适的运算符:{0}", this.Operation); } } }
在解决方案中创建windows form项目
设计一个简单的计算器的界面
做代码如下
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; using System.Workflow.Runtime; using System.Threading; using WorkflowLibrary1; namespace test { public partial class Form1 : Form { private WorkflowRuntime _wfRuntime; private AutoResetEvent _waitHandle = new AutoResetEvent(false);//已发生事件 private string _result; public Form1() { InitializeComponent(); initWFRuntime();//一个appdomain只能初始化一次 } /// <summary> /// 工作流初始化 /// </summary> private void initWFRuntime() { _wfRuntime = new WorkflowRuntime(); _wfRuntime.WorkflowCompleted += new EventHandler<WorkflowCompletedEventArgs>(_wfRuntime_WorkflowCompleted); _wfRuntime.WorkflowTerminated += new EventHandler<WorkflowTerminatedEventArgs>(_wfRuntime_WorkflowTerminated); } void _wfRuntime_WorkflowTerminated(object sender, WorkflowTerminatedEventArgs e) { MessageBox.Show(e.Exception.Message); _waitHandle.Set();//发生事件已终止 } void _wfRuntime_WorkflowCompleted(object sender, WorkflowCompletedEventArgs e) { _result = e.OutputParameters["Result"].ToString(); _waitHandle.Set(); } private void getResult() { Dictionary<string, object> dic = new Dictionary<string, object>(); dic.Add("Operation", textBox2.Text.Trim()); dic.Add("Number2", int.Parse(textBox3.Text.Trim())); dic.Add("Number1", int.Parse(textBox1.Text.Trim())); WorkflowInstance wfInstance = _wfRuntime.CreateWorkflow(typeof(WorkflowLibrary1.Workflow1), dic); wfInstance.Start(); _waitHandle.WaitOne(); } /// <summary> /// 注册按钮事件 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void Form1_Load(object sender, EventArgs e) { foreach (var ctl in this.Controls) { if (ctl is Button) { ((Button)ctl).Click += new EventHandler(Form1_Click); } } } /// <summary> /// 按钮事件 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> void Form1_Click(object sender, EventArgs e) { Button btn = (Button)sender; TextBox tb; if (string.IsNullOrEmpty(textBox1.Text.Trim())) { tb = textBox1; } else if (string.IsNullOrEmpty(textBox3.Text.Trim())) { tb = textBox3; } else { tb = textBox1; } switch (btn.Text) { case "1": case "2": case "3": case "4": case "5": case "6": case "7": case "8": case "9": case "0": tb.Text = btn.Text; break; case "+": case "-": case "*": case "/": textBox2.Text = btn.Text; break; case "=": getResult(); textBox4.Text = _result; break; default: break; } } } }