title | description | ms.date | ms.assetid |
---|---|---|---|
Creating an Activity at Runtime with DynamicActivity |
DynamicActivity is a concrete, sealed class with a public constructor. Use the class to assemble activity functionality at run time using an activity DOM. |
03/30/2017 |
1af85cc6-912d-449e-90c5-c5db3eca5ace |
xref:System.Activities.DynamicActivity is a concrete, sealed class with a public constructor. xref:System.Activities.DynamicActivity can be used to assemble activity functionality at run time using an activity DOM.
xref:System.Activities.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 xref:System.Activities.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 xref:System.Activities.DynamicActivity interface with the designer using xref:System.ComponentModel.ICustomTypeDescriptor. Activities created in the designer can be loaded dynamically using xref:System.Activities.XamlIntegration.ActivityXamlServices.Load%2A, as demonstrated in the following procedure.
-
OpenVisual Studio 2010.
-
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.
-
Right-click Workflow1.xaml in the HelloActivity project and select Delete.
-
Open Program.cs. Add the following directive to the top of the file.
using System.Collections.Generic;
-
Replace the contents of the
Main
method with the following code, which creates a xref:System.Activities.Statements.Sequence activity that contains a single xref:System.Activities.Statements.WriteLine activity and assigns it to the xref:System.Activities.DynamicActivity.Implementation%2A 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();
-
Execute the application. A console window with the text "Hello World!" displays.
-
Open Visual Studio 2010.
-
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.
-
Open Workflow1.xaml in the HelloActivity project. Click the Arguments option at the bottom of the designer. Create a new
In
argument calledTextToWrite
of typeString
. -
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. -
Open Program.cs. Add the following directive to the top of the file.
using System.Activities.XamlIntegration;
-
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();
-
Execute the application. A console window with the text "Hello World!" appears.
-
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 withx:Property
.