Exercise 4: Dynamic Workflows with XAML

Up to this point, you have been authoring workflows in .xaml, .cs or .vb files. These files are compiled into types that are included in the assembly of the project and run by the workflow runtime.

While it might look like the format of the source files does not matter, .xaml files offer some distinct advantages over authoring workflows in C# or VB.

  • The Workflow Designer works with .xaml files only, workflows authored in C# or VB have no designer support.
  • XAML can be loaded and run dynamically without compiling it into an assembly

Dynamic workflows provide some interesting possibilities for programs that want to generate business logic or make a runtime decision on which business logic to load and run.

Task 0 – Opening the Solution

To begin this exercise you can use the solution you finished from Exercise 3. Alternatively, you can follow the following steps to begin with Exercise 4.

  1. Start Microsoft Visual Studio 2010 from Start | All Programs | Microsoft Visual Studio 2010.
  2. Open the starting solution for Exercise 4 located under the Source\Ex4-XAML\Begin folder and use it as the starting point for this exercise.
  3. Press CTRL+SHIFT+B to build the solution.

Task 1 – Modifying SayHello.xaml Files Properties

In this task you will modify the HellowWorkflow program to load and run the SayHello.xaml file. Then you will modify the text of SayHello.xaml and observe the change in the message the next time you run the application.

  1. You need to tell Visual Studio to treat SayHello.xaml as deployed content rather than code. To do this
    1. In Solution Explorer select SayHello.xaml
    2. Build Action: Content
    3. Copy To Output Directory: Copy Always
    4. Set the Custom Tool to blank

      Figure 13

      Change the properties of SayHello.xaml to treat it as content

Task 2 – Modifying Main() to Load the SayHello.xaml File

Previously your workflow was compiled into a type. To invoke a workflow from a .xaml file you will need to use ActivityXamlServices to load the .xaml file into memory and create an instance of an activity that WorkflowInvoker can invoke. Keep in mind that any assemblies that your .xaml file references must be available when the workflow is invoked.

  1. Add the following namespace directives to the program.cs or Module1.vb file.

    C#

    using System.Activities.XamlIntegration;

    Visual Basic

    Imports System.Activities.XamlIntegration

  2. Modify program.cs or Module1.vb to use ActivityXamlServices and also add a call to Console.ReadKey this will make it easier to see what is happening with your app when running it from Windows Explorer.

    (Code Snippet - Introduction to WF4 Lab - ActivityXamlServices CSharp)

    C#

    static void Main(string[] args) { WorkflowInvoker.Invoke(ActivityXamlServices.Load("SayHello.xaml")); Console.ReadKey(false); }

    (Code Snippet - Introduction to WF4 Lab - ActivityXamlServices VB)

    Visual Basic

    Shared Sub Main() WorkflowInvoker.Invoke(ActivityXamlServices.Load("SayHello.xaml")) Console.ReadKey(False) End Sub

Next Step

Exercise 4: Verification