一个例子(Hello World)

Hello World ,向前辈致敬!

该篇作为WWF_designer部份的正式开篇文章

本例中,将在一个XML文件中设计工作流,让引擎用该XML文件创建并运行实例

第一步,新建一个名为[控件]的空项目,添加一个Activity项


第二步,简单的添加点东西,演示吗,加点什么随便


代码如下

Public Class Activity1
    
Inherits SequenceActivity
    
Public Sub New()
        
MyBase.New()
        InitializeComponent()
    
End Sub


    
Public Shared 自定义属性Property As DependencyProperty = DependencyProperty.Register("自定义属性"GetType(System.String), GetType(Activity1))

    
<DescriptionAttribute("这是一个自定义属性")> _
    
<DesignerSerializationVisibilityAttribute(DesignerSerializationVisibility.Visible)> _
    
<BrowsableAttribute(True)> _
    
Public 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


    
Private Sub codeActivity1_ExecuteCode(ByVal sender As System.Object, ByVal e As System.EventArgs)
        Console.WriteLine(
"hello world (^_^)")
        Console.WriteLine(自定义属性)
    
End Sub

End Class

第三步,编译,然后关闭项目,生成的[控件.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

Public Class Class1
    
Shared WaitHandle As New AutoResetEvent(False)

    
Shared Sub Main()
        
Dim 文件 As String = "workflow.xml"

        
Dim 引擎 As New WorkflowRuntime()
        
AddHandler 引擎.WorkflowCompleted, AddressOf OnWorkflowCompleted
        
AddHandler 引擎.WorkflowTerminated, AddressOf OnWorkflowTerminated

        引擎.StartRuntime()

        
Dim 实例 As WorkflowInstance = Nothing
        
Try
            
Dim 读取对象 As XmlReader = XmlReader.Create(文件)
            实例 
= 引擎.CreateWorkflow(读取对象)

            读取对象.Close()

        
Catch 异常 As WorkflowValidationFailedException
            
Dim 错误列表 As ValidationErrorCollection = 异常.Errors

            
For Each temp As ValidationError In 错误列表
                Console.WriteLine(temp.ErrorText)
            
Next
            Console.Read()
            
Return
        
End Try


        Console.WriteLine(
"准备启动流程")

        实例.Start()

        WaitHandle.WaitOne()

        Console.Read()
    
End Sub


    
Shared Sub OnWorkflowCompleted(ByVal sender As ObjectByVal e As WorkflowCompletedEventArgs)
        Console.WriteLine(
"工作流完成")
        WaitHandle.Set()
    
End Sub


    
Shared Sub OnWorkflowTerminated(ByVal sender As ObjectByVal e As WorkflowTerminatedEventArgs)
        Console.WriteLine(
"运行出错:" + e.Exception.Message)
        WaitHandle.Set()
    
End Sub


End Class

编译,生成[宿主.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]文件


修改[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]文件






代码下载  一个例子(Hello World).rar

原文地址:https://www.cnblogs.com/foundation/p/530925.html