Hello World ,向前辈致敬!
该篇作为WWF_designer部份的正式开篇文章
本例中,将在一个XML文件中设计工作流,让引擎用该XML文件创建并运行实例
第一步,新建一个名为[控件]的空项目,添加一个Activity项
![](https://images.cnblogs.com/cnblogs_com/foundation/kj1.JPG)
第二步,简单的添加点东西,演示吗,加点什么随便
![](https://images.cnblogs.com/cnblogs_com/foundation/kj2.JPG)
代码如下
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
Public Class Activity1Class Activity1
Inherits SequenceActivity
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
Public Sub New()Sub New()
MyBase.New()
InitializeComponent()
End Sub
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
Public Shared 自定义属性Property As DependencyProperty = DependencyProperty.Register("自定义属性", GetType(System.String), GetType(Activity1))
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
<DescriptionAttribute("这是一个自定义属性")> _
<DesignerSerializationVisibilityAttribute(DesignerSerializationVisibility.Visible)> _
<BrowsableAttribute(True)> _
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
Public Property 自定义属性()Property 自定义属性() As String
Get
Return CType(MyBase.GetValue(自定义属性Property), String)
End Get
Set(ByVal value As String)
MyBase.SetValue(自定义属性Property, value)
End Set
End Property
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
Private Sub codeActivity1_ExecuteCode()Sub codeActivity1_ExecuteCode(ByVal sender As System.Object, ByVal e As System.EventArgs)
Console.WriteLine("hello world (^_^)")
Console.WriteLine(自定义属性)
End Sub
End Class
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
第三步,编译,然后关闭项目,生成的[控件.DLL]文件放到一旁备用
再新建一个控制台项目[宿主],代码如下
Imports System
Imports System.ComponentModel.Design.Serialization
Imports System.Threading
Imports System.Workflow.Activities
Imports System.Workflow.ComponentModel.Compiler
Imports System.Workflow.ComponentModel.Serialization
Imports System.Workflow.Runtime
Imports System.Xml
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
Public Class Class1Class Class1
Shared WaitHandle As New AutoResetEvent(False)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
Shared Sub Main()Sub Main()
Dim 文件 As String = "workflow.xml"
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
Dim 引擎 As New WorkflowRuntime()
AddHandler 引擎.WorkflowCompleted, AddressOf OnWorkflowCompleted
AddHandler 引擎.WorkflowTerminated, AddressOf OnWorkflowTerminated
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
引擎.StartRuntime()
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
Dim 实例 As WorkflowInstance = Nothing
Try
Dim 读取对象 As XmlReader = XmlReader.Create(文件)
实例 = 引擎.CreateWorkflow(读取对象)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
读取对象.Close()
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
Catch 异常 As WorkflowValidationFailedException
Dim 错误列表 As ValidationErrorCollection = 异常.Errors
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
For Each temp As ValidationError In 错误列表
Console.WriteLine(temp.ErrorText)
Next
Console.Read()
Return
End Try
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
Console.WriteLine("准备启动流程")
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
实例.Start()
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
WaitHandle.WaitOne()
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
Console.Read()
End Sub
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
Shared Sub OnWorkflowCompleted()Sub OnWorkflowCompleted(ByVal sender As Object, ByVal e As WorkflowCompletedEventArgs)
Console.WriteLine("工作流完成")
WaitHandle.Set()
End Sub
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
Shared Sub OnWorkflowTerminated()Sub OnWorkflowTerminated(ByVal sender As Object, ByVal e As WorkflowTerminatedEventArgs)
Console.WriteLine("运行出错:" + e.Exception.Message)
WaitHandle.Set()
End Sub
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
End Class
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
编译,生成[宿主.exe]文件,然后关闭项目
将[控件.DLL]文件复制到上步生成的[宿主.exe]文件所在的目录,
在该目当中新建一个XML文件[workflow.xml]
<?xml version="1.0" encoding="utf-8"?><SequentialWorkflowActivity x:Name="wxwinter" Description="一个控件" xmlns:ns0="clr-namespace:控件;Assembly=控件, 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:Activity1 x:Name="wxd1" 自定义属性="wf是一个不错的工作流平台" />
</SequentialWorkflowActivity>
运行[宿主.exe]文件
![](https://images.cnblogs.com/cnblogs_com/foundation/aa2.JPG)
修改[workflow.xml]
<?xml version="1.0" encoding="utf-8"?><SequentialWorkflowActivity x:Name="wxwinter" Description="一个控件" xmlns:ns0="clr-namespace:控件;Assembly=控件, 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:Activity1 x:Name="wxd1" 自定义属性="wf是一个不错的工作流平台" />
<ns0:Activity1 x:Name="wxd2" 自定义属性="功能很强大" />
<ns0:Activity1 x:Name="wxd3" 自定义属性="不知何时能出正式版" />
<ns0:Activity1 x:Name="wxd4" 自定义属性="也许还得等十年" />
</SequentialWorkflowActivity>
运行[宿主.exe]文件
![](https://images.cnblogs.com/cnblogs_com/foundation/aa1.JPG)
代码下载 一个例子(Hello World).rar