Tuesday, October 23, 2012

Dynamic Workflows with XAML

Up to this point we have been authoring workflows in .xaml, .cs or .vb files. These files then get 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.

From last article The CodeActivity we continue our project, we will modify the HellowWorkflow program to load and run the SayHello.xaml file. Then we will modify the text of SayHello.xaml and observe the change in the message the next time we run the application.
  1. We need to tell Visual Studio to treat SayHello.xaml as content that must be deployed rather than code that must be built. To do this.

    a.       In Solution Explorer select SayHello.xamlb.       In the Properties windows, set the build action to Contentc.       Set the Copy To Output file property on SayHello.xaml to Copy if newerd.       Set the Custom Tool to blank
    Change the properties of SayHello.xaml to treat it as content
    Change the properties of SayHello.xaml to treat it as content
Previously our class was compiled into a type. To invoke a workflow from a .xaml file we will need to use ActivityXamlServices to load the .xaml file into memory and create an instance of System.Activities.DynamicActivity that WorkflowInvoker can invoke. Keep in mind that any assemblies that our .xaml file references must be available when the workflow is invoked.
  1. Add the following namespace directives to the file.
    C#
    using System.Activities.XamlIntegration;

    Visual Basic
    Imports System.Activities.XamlIntegration
  2. Modify program.cs to use ActivityXamlServices and also add a call to Console.ReadKey this will make it easier to see what is happening with our app when running it from Windows Explorer. To do this, replace the Main method implementation with the following code:
    C#
    static void Main(string[] args)
    {
      WorkflowInvoker.Invoke(ActivityXamlServices.Load("SayHello.xaml"));
      Console.ReadKey(false);
    }

    Visual Basic
    Shared Sub Main()
      WorkflowInvoker.Invoke(ActivityXamlServices.Load("SayHello.xaml"))
      Console.ReadKey(False)
    End Sub
  3. Press CTRL+F5 to run the workflow without debugging. The application should run in a console window and print the message “Hello Workflow 4".
  4. Navigate to the Bin\Debug directory under your project folder and locate SayHello.xaml.
    Locate the SayHello.xaml file in the project’s Bin\Debug directory
    Locate the SayHello.xaml file in the project’s Bin\Debug directory
    1. In Notepad change the Text property of the WriteLine activity to “Hello Workflow 4 XAML” then save and close it.
      Change the Text property in Notepad
      Change the Text property in Notepad
      1. In Windows Explorer run HelloWorkflow.exe and it should now say “Hello Workflow 4 from XAML”. Press any key to exit.
        HelloWorkflow.exe showing a new message from the .xaml file
        HelloWorkflow.exe showing a new message from the .xaml file

        No comments:

        Post a Comment

        Automatic Traffic Exchange

        YallaTech Facebook page