语言:C#
平台:.NET 3.5
1:如下图,首先创建一个空项目,命名为 HelloWordControlWF。解决方案名称为WFHelloWord
2:重命名WorkFlow1.cs 为HelloWord.cs
3:添加一个codeActivity1
3:双击codeActivity1 添加codeActivity1_ExecuteCode 事件
4:添加依赖属性OutDependency,完整代码
using System;
using System.ComponentModel;
using System.ComponentModel.Design;
using System.Collections;
using System.Drawing;
using System.Linq;
using System.Workflow.ComponentModel;
using System.Workflow.ComponentModel.Design;
using System.Workflow.ComponentModel.Compiler;
using System.Workflow.ComponentModel.Serialization;
using System.Workflow.Runtime;
using System.Workflow.Activities;
using System.Workflow.Activities.Rules;
namespace HelloWordControlWF
{
public partial class HelloWord : SequenceActivity
{
public HelloWord()
{
InitializeComponent();
}
public static DependencyProperty OutDependencyProperty = DependencyProperty.Register("OutDependency", typeof(string), typeof(HelloWord));
[DescriptionAttribute("OutDependency")]
[CategoryAttribute("OutDependency Category")]
[BrowsableAttribute(true)]
[DesignerSerializationVisibilityAttribute(DesignerSerializationVisibility.Visible)]
public string OutDependency
{
get
{
return ((string)(base.GetValue(HelloWord.OutDependencyProperty)));
}
set
{
base.SetValue(HelloWord.OutDependencyProperty, value);
}
}
private void codeActivity1_ExecuteCode(object sender, EventArgs e)
{
Console.WriteLine("hello world (^_^)");
Console.WriteLine(OutDependency);
}
}
}
5:添加一个顺序工作流控制台应用程序
6:删除WorkFlow1.cs 本项目不需要
7:对program.cs 文件进行编辑,代码如下:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Workflow.Runtime;
using System.Workflow.Runtime.Hosting;
using System.Xml;
using System.Workflow.Activities;
using System.Workflow.ComponentModel.Compiler;
namespace HelloWordConsole
{
class Program
{
static void Main(string[] args)
{
using(WorkflowRuntime workflowRuntime = new WorkflowRuntime())
{
AutoResetEvent waitHandle = new AutoResetEvent(false);
workflowRuntime.WorkflowCompleted += delegate(object sender, WorkflowCompletedEventArgs e)
{
Console.WriteLine("WorkFlow finished");
waitHandle.Set();
};
workflowRuntime.WorkflowTerminated += delegate(object sender, WorkflowTerminatedEventArgs e)
{
Console.WriteLine("Running With Exception:" + e.Exception.Message);
waitHandle.Set();
};
WorkflowInstance instance = null;
try
{
string xmlPath = @"..\\..\\workflow.xml";
XmlReader reader = XmlReader.Create(xmlPath);
instance = workflowRuntime.CreateWorkflow(reader);
reader.Close();
}
catch (WorkflowValidationFailedException e)
{
ValidationErrorCollection errList = e.Errors;
foreach (ValidationError err in errList)
{
Console.WriteLine(err.ErrorText);
}
Console.Read();
return;
}
Console.WriteLine("Ready to start Hello Word WorkFolw");
instance.Start();
waitHandle.WaitOne();
Console.Read();
}
}
}
}
8:添加对HelloWordControlWF的引用
9:添加一个XML文件并且重命名为workflow.xml。内容如下:
<?xml version="1.0" encoding="utf-8" ?>
<SequentialWorkflowActivity x:Name="Char" Description="HelloWord" xmlns:ns0="clr-namespace:HelloWordControlWF;Assembly=HelloWordControlWF, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/workflow">
<ns0:HelloWord x:Name="helloword1" OutDependency="wf is a good platform 1" />
<ns0:HelloWord x:Name="helloword2" OutDependency="wf is a good platform 2" />
<ns0:HelloWord x:Name="helloword3" OutDependency="wf is a good platform 3" />
<ns0:HelloWord x:Name="helloword4" OutDependency="wf is a good platform 4" />
</SequentialWorkflowActivity>
10:编译,运行结果如下:
源代码下载:WFHelloWord.zip