How to: Create a Custom Activity Template
Custom activity templates are used to customize the configuration of activities, including custom composite activities, so that users do not have to create each activity individually and configure their properties and other settings manually. These custom templates can be made available in the Toolbox on the Windows Workflow Designer or from a rehosted designer, from which users can drag them onto the preconfigured design surface. Workflow Designer ships with good examples of such templates: the SendAndReceiveReply Template Designer and the ReceiveAndSendReply Template Designer in the Messaging Activity Designers category.
The first procedure in this topic describes how to create a custom activity template for a Delay activity and the second procedure describes briefly how to make it available in a Workflow Designer to verify that the custom template works.
Custom activity templates must implement the IActivityTemplateFactory. The interface has a single Create method with which you can create and configure the activity instances used in the template.
To create a template for the Delay activity
Start Visual Studio 2010.
On the File menu, point to New, and then select Project.
The New Project dialog box opens.
In the Project Types pane, select Workflow from either the Visual C# projects or Visual Basic groupings depending on your language preference.
In the Templates pane, select Activity Library.
In the Name box, enter
DelayActivityTemplate
.Accept the defaults in the Location and Solution name text boxes, and then click OK.
Right-click the References directory of the DelayActivityTemplate project in Solution Explorer and choose Add Reference to open the Add Reference dialog box.
Go to the .NET tab and select PresentationFramework from the Component Name column on the left and click OK to add a reference to the PresentationFramework.dll file.
Repeat this procedure to add references to the System.Activities.Presentation.dll and the WindowsBase.dll files.
Right-click the DelayActivityTemplate project in Solution Explorer and choose Add and then New Item to open the Add New Item dialog box.
Select the Class template, name it MyDelayTemplate, and then click OK.
Open the MyDelayTemplate.cs file and add the following statements.
//Namespaces added using System.Activities; using System.Activities.Statements; using System.Activities.Presentation; using System.Windows;
Implement the IActivityTemplateFactory with the
MyDelayActivity
class with the following code. This configures the delay to have a duration of 10 seconds.public sealed class MyDelayActivity : IActivityTemplateFactory { public Activity Create(System.Windows.DependencyObject target) { return new System.Activities.Statements.Delay { DisplayName = "DelayActivityTemplate", Duration = new TimeSpan(0, 0, 10) }; } }
Select Build Solution from the Build menu to generate the DelayActivityTemplate.dll file.
To make the template available in a Workflow Designer
Right-click the DelayActivityTemplate solution in Solution Explorer and choose Add and then New Project to open the Add New Project dialog box.
Select the Workflow Console Application template, name it
CustomActivityTemplateApp
, and then click OK.Right-click the References directory of the CustomActivityTemplateApp project in Solution Explorer and choose Add Reference to open the Add Reference dialog box.
Go to the Projects tab and select DelayActivityTemplate from the Project Name column on the left and click OK to add a reference to the DelayActivityTemplate.dll file that you created in the first procedure.
Right-click the CustomActivityTemplateApp project in Solution Explorer and choose Build to compile the application.
Right-click the CustomActivityTemplateApp project in Solution Explorer and choose Set as Startup Project.
Select Start Without Debugging from the Debug menu and press any key to continue when prompted from the cmd.exe window.
Open the Workflow1.xaml file and open the Toolbox.
Locate the MyDelayActivity template in the DelayActivityTemplate category. Drag it onto the design surface. Confirm in the Properties window that the
Duration
property has been set to 10 seconds.
Example
The MyDelayActivity.cs file should contain the following code.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
//Namespaces added
using System.Activities;
using System.Activities.Statements;
using System.Activities.Presentation;
using System.Windows;
namespace DelayActivityTemplate
{
public sealed class MyDelayActivity : IActivityTemplateFactory
{
public Activity Create(System.Windows.DependencyObject target)
{
return new System.Activities.Statements.Delay
{
DisplayName = "DelayActivityTemplate",
Duration = new TimeSpan(0, 0, 10)
};
}
}
}