Muokkaa

Jaa


Creating an Activity at Runtime with DynamicActivity

DynamicActivity is a concrete, sealed class with a public constructor. DynamicActivity can be used to assemble activity functionality at run time using an activity DOM.

DynamicActivity Features

DynamicActivity has access to execution properties, arguments and variables, but no access to run-time services such as scheduling child activities or tracking.

Top-level properties can be set using workflow Argument objects. In imperative code, these arguments are created using CLR properties on a new type. In XAML, they are declared using x:Class and x:Member tags.

Activities constructed using DynamicActivity interface with the designer using ICustomTypeDescriptor. Activities created in the designer can be loaded dynamically using Load, as demonstrated in the following procedure.

To create an activity at run time using imperative code

  1. OpenVisual Studio 2010.

  2. Select File, New, Project. Select Workflow 4.0 under Visual C# in the Project Types window, and select the v2010 node. Select Sequential Workflow Console Application in the Templates window. Name the new project DynamicActivitySample.

  3. Right-click Workflow1.xaml in the HelloActivity project and select Delete.

  4. Open Program.cs. Add the following directive to the top of the file.

    using System.Collections.Generic;  
    
  5. Replace the contents of the Main method with the following code, which creates a Sequence activity that contains a single WriteLine activity and assigns it to the Implementation property of a new dynamic activity.

    //Define the input argument for the activity  
    var textOut = new InArgument<string>();  
    //Create the activity, property, and implementation  
                Activity dynamicWorkflow = new DynamicActivity()  
                {  
                    Properties =
                    {  
                        new DynamicActivityProperty  
                        {  
                            Name = "Text",  
                            Type = typeof(InArgument<String>),  
                            Value = textOut  
                        }  
                    },  
                    Implementation = () => new Sequence()  
                    {  
                        Activities =
                        {  
                            new WriteLine()  
                            {  
                                Text = new InArgument<string>(env => textOut.Get(env))  
                            }  
                        }  
                    }  
                };  
    //Execute the activity with a parameter dictionary  
                WorkflowInvoker.Invoke(dynamicWorkflow, new Dictionary<string, object> { { "Text", "Hello World!" } });  
                Console.ReadLine();  
    
  6. Execute the application. A console window with the text "Hello World!" displays.

To create an activity at run time using XAML

  1. Open Visual Studio 2010.

  2. Select File, New, Project. Select Workflow 4.0 under Visual C# in the Project Types window, and select the v2010 node. Select Workflow Console Application in the Templates window. Name the new project DynamicActivitySample.

  3. Open Workflow1.xaml in the HelloActivity project. Click the Arguments option at the bottom of the designer. Create a new In argument called TextToWrite of type String.

  4. Drag a WriteLine activity from the Primitives section of the toolbox onto the designer surface. Assign the value TextToWrite to the Text property of the activity.

  5. Open Program.cs. Add the following directive to the top of the file.

    using System.Activities.XamlIntegration;  
    
  6. Replace the contents of the Main method with the following code.

    Activity act2 = ActivityXamlServices.Load(@"Workflow1.xaml");  
                    results = WorkflowInvoker.Invoke(act2, new Dictionary<string, object> { { "TextToWrite", "HelloWorld!" } });  
    Console.ReadLine();  
    
  7. Execute the application. A console window with the text "Hello World!" appears.

  8. Right-click the Workflow1.xaml file in the Solution Explorer and select View Code. Note that the activity class is created with x:Class and the property is created with x:Property.

See also