Share via


Using Workflow MarkupĀ 

Windows Workflow Foundation gives designers and developers a declarative way to create workflows by using eXtensible Application Markup Language (XAML) to create markup source files. These markup files can be compiled into a workflow type, loaded directly into the workflow runtime engine at run time, or they can be compiled into a workflow type with code-behind files implemented in either C# or Visual Basic. This means that the workflow markup files can be compiled or not, depending on business reasons and whether additional implementation logic is required. The use of workflow markup with code-behind logic files is similar to how ASP.NET separates presentation files from logic files.

For an example of how to load a workflow markup file directly into the workflow runtime engine, see the Running Workflows section of the Creating a Workflow Host Application topic.

Besides creating a workflow in workflow markup, you can also use workflow markup when persisting your workflow with a WorkflowPersistenceService service because when programmatic workflows are serialized with the WorkflowMarkupSerializer, they become workflow markup. For more information, see How to: Serialize Workflows and Windows Workflow Persistence Services.

Basic structure

The basic structure of the workflow markup contains the root node, which denotes the type of workflow, followed by the child activities in that workflow as nested sub elements. Because workflow markup is based on a subset of XAML elements and attributes, the structure of workflow markup files looks similar to that of an XAML file. For example, each element in the workflow is represented as nodes of either composite activities or the workflow itself. The relationship between nodes is preserved just like it is when you create workflows through a programming language such as C# or Visual Basic.

Elements and Attributes

As stated previously, each element in a workflow markup file corresponds to a workflow component. The names for those elements are the same names for the activity types that are used when you create workflows programmatically. For example, the IfElseActivity activity is represented by the <IfElseActivity> element. This is also true for custom activities.

Activity members are declared as shown in the following example:

<SampleActivity Property1="PropValue" Method="CustomMethod" Event="EventHandlerMethod"/>

XAML also provides the ability to insert procedural code within a workflow markup file using the x:Code directive element. The code must be placed in a CDATA section so that the compiler can compile the code instead of treating it like declarative XAML markup. The following example shows how that element is used with a CDATA section.

<CodeActivity x:Name="codeActivity1" ExecuteCode="methodName1">
  <x:Code><![CDATA[
      void methodName1(object sender, EventArgs e) 
      {
      }
  ]]></x:Code>
</CodeActivity>
NoteNote

The x:Code directive element can only be used in workflow markup files that are compiled.

The following table describes the common attributes of workflow markup.

Attribute Description

x:Array

An array of types.

x:Class

Name of the workflow class including the namespace. The class with this name is created when the workflow is compiled.

x:Name

Name of an activity. Corresponds to the Activity.Name property.

x:Type

A type reference.

x:Null

A null value.

xmlns:x

The namespace for the XAML schema.

xmlns

The namespace for the workflow XAML schema.

NoteNote

If you use a non-compiled, XAML-only workflow markup file to create a workflow, x:Class attribute should not be present in the XAML file. This attribute is only valid when the workflow is being compiled.

NoteNote

If the Root Namespace for a VB application is changed after a workflow markup file is created, the x:Class attribute of that workflow must also be updated.

Example

The following is an example of a workflow markup file that is used with a code-behind file that contains implementation logic for the various event handlers in the workflow.

<SequentialWorkflowActivity x:Class="XAMLWorkflow.Workflow1" x:Name="Workflow1" xmlns:x="https://schemas.microsoft.com/winfx/2006/xaml" xmlns="https://schemas.microsoft.com/winfx/2006/xaml/workflow">
    <IfElseActivity x:Name="ifElseActivity1">
        <IfElseBranchActivity x:Name="ifElseBranchActivity1">
            <IfElseBranchActivity.Condition>
                <CodeCondition Condition="EvalCondition" />
            </IfElseBranchActivity.Condition>
            <CodeActivity x:Name="codeActivity1" ExecuteCode="codeActivity1_ExecuteCode" />
            <FaultHandlersActivity x:Name="faultHandlersActivity1">
                <FaultHandlerActivity x:Name="faultHandlerActivity1" Fault="{ActivityBind Workflow1,Path=faultHandlerProp}" FaultType="{x:Type System.NullReferenceException}">
                    <CodeActivity x:Name="codeActivity3" ExecuteCode="codeActivity3_ExecuteCode" />
                </FaultHandlerActivity>
            </FaultHandlersActivity>
        </IfElseBranchActivity>
        <IfElseBranchActivity x:Name="ifElseBranchActivity2">
            <CodeActivity x:Name="codeActivity2" ExecuteCode="codeActivity2_ExecuteCode" />
        </IfElseBranchActivity>
    </IfElseActivity>
</SequentialWorkflowActivity>

You must implement the Condition and ExecuteCode event handlers before this example will compile successfully, as in the following example:

private void EvalCondition(object sender, ConditionalEventArgs e) { }
private void codeActivity1_ExecuteCode(object sender, EventArgs e) { }
private void codeActivity2_ExecuteCode(object sender, EventArgs e) { }
NoteNote

If you use a non-compiled, XAML-only workflow markup file to create a workflow, you must use ActivityBind markup extension to set all dependency properties of type event handler or they will not be called during run time. See the following example:

<CodeActivity x:Name="codeActivity1" ExecuteCode="{ActivityBind Name=Activity12, Path=codeActivity1_ExecuteCode}" />

See Also

Reference

System.Windows.Markup

Concepts

Using Custom Activities with Workflow Markup
Using Rules with Workflow Markup
How to: Serialize Workflows
Serialization Overview

Other Resources

Markup Samples
Developing Workflows

Footer image

Send comments about this topic to Microsoft.