/Home /Archive /Syndicate /Blog /Support /About /Contact  
All Visual Basic Feeds in one place!





I have been experimenting a bit with hosting the workflow designer. Unfortunately the sample provided, while it works is kind of hard to get started with if you are not already familiar with the way designers work in Visual Studio. So I decided to create the minimal code to create an image of a workflow, something quite useful if you are going to with an ASP.NET site to monitor you workflows. One caveat to be aware of is that the code needs to run under a thread with an STA ApartmentState. If not you will receive errors messages that the DragDrop registration did not succeed while creating the WorkflowView.
 
The code is pretty simple. Basically it uses a standard System.ComponentModel.Design.DesignSurface and System.Workflow.ComponentModel.Design.WorkflowView type to work with the workflow. To load the workflow a custom WorkflowDesignerLoader subclass is required to do the actual loading. No big deal. If you are using a State Workflow you need to override the OnEndLoad function to load the .layout file and use the LoadDesignerLayout function to add it to the image generated.
 
Imports System.ComponentModel.Design
Imports System.ComponentModel.Design.Serialization
Imports System.Drawing.Imaging
Imports System.Workflow.Activities
Imports System.Workflow.ComponentModel
Imports System.Workflow.ComponentModel.Design
 
Module Module1
    Sub Main()
        Dim workflow AsNew SequentialWorkflowActivity
        workflow.Activities.Add(New DelayActivity())
 
        Dim loader AsNew WorkflowLoader(workflow)
        Dim surface AsNew DesignSurface
        surface.BeginLoad(loader)
        Dim view AsNew WorkflowView(CType(surface, IServiceProvider))
        view.SaveWorkflowImage("workflow.png", ImageFormat.Png)
 
        Process.Start("workflow.png")
    EndSub
EndModule
 
 
PublicClass WorkflowLoader
    Inherits WorkflowDesignerLoader
 
    Private _workflowDefinition As Activity
 
    SubNew(ByVal workflowDefinition As Activity)
        _workflowDefinition = workflowDefinition
    EndSub
 
    ProtectedOverridesSub PerformLoad(ByVal serializationManager As IDesignerSerializationManager)
        MyBase.PerformLoad(serializationManager)
 
        Dim designerHost As IDesignerHost = Me.GetService(GetType(IDesignerHost))
        Dim allActivities As List(Of Activity) = WorkflowUtils.GetAllActivities(_workflowDefinition)
 
        ForEach item As Activity In allActivities
            designerHost.Container.Add(item, item.QualifiedName)
        Next
    EndSub
 
    PublicOverridesReadOnlyProperty FileName() AsString
        Get
            Return""
        EndGet
    EndProperty
 
    PublicOverridesFunction GetFileReader(ByVal filePath AsString) As System.IO.TextReader
        ThrowNew NotSupportedException()
    EndFunction
 
    PublicOverridesFunction GetFileWriter(ByVal filePath AsString) As System.IO.TextWriter
        ThrowNew NotSupportedException()
    EndFunction
EndClass
 
Enjoy!
 
Update: Continued here.
 
Share this post: email it! | bookmark it! | digg it! | live it!
© 2005 Serge Baranovsky. All rights reserved.
All feed content is property of original publisher. Designated trademarks and brands are the property of their respective owners.

This site is maintained by SubMain(), a division of vbCity.com, LLC