Activity Verbs

This article will show you how to add custom verbs to an activity designer - these are surfaced as menu items from the popup menu of a given activity, and are also added to the main menu structure of Visual Studio. The placement of the menu item can be chosen using an enumerated constant. The image below shows a custom verb which has been added to the menu for an activity...

In order to add custom verbs to an activity there are two main steps you need to take...

  • Create a custom designer for your activity
  • Override the Verbs property and add in your custom verbs

The code below shows an implementation of the designer for the activity shown above...

public class CustomActivityDesigner : ActivityDesigner
{
  protected override ActivityDesignerVerbCollection Verbs
  {
    get
    {
      ActivityDesignerVerbCollection verbs = base.Verbs;

      if (null == _customVerbs)
      {
        _customVerbs = new ActivityDesignerVerbCollection();
        _customVerbs.Add(new ActivityDesignerVerb(this, DesignerVerbGroup.Actions, "Click Me Please!", new EventHandler(CustomHandler)));
      }

      verbs.AddRange(_customVerbs);

      return verbs;
    }
  }

  private void CustomHandler(object sender, EventArgs e)
  {
    MessageBox.Show("You clicked me!");
  }

  private ActivityDesignerVerbCollection _customVerbs;
}

Here I have overridden the Verbs property and returned an augmented collection of verbs. The DesignerVerbGroup enumeration defines where in the menu the verb shows up, which is outlined in the screenshots below. In these screenshots I have used the name of the enumerated constant to display where in the menu a verb using that constant would show up.

The image below shows the contents of the Workflow menu.

And this image shows the contents of the context sensitive menu.

When a custom verb is added to the menu structure a separator is added as needed, so you don't need to add separators yourself as this is handled for you. The handler in your code must conform to the standard event handler signature and it is passed the verb as the sender argument.

  private void CustomHandler(object sender, EventArgs e)
  {
    ActivityDesignerVerb verb = sender as ActivityDesignerVerb ;
    if ( null != verb )
    {
      // Do stuff here...
    }
  }

  private ActivityDesignerVerbCollection _customVerbs;
}

The ActivityDesignerVerb object contains nothing particularly useful, other than the ActivityDesigner which is unfortunately private, so you can't get hold of it unless you use some Reflection. For this reason I would suggest that if you need to get hold of the activity within the event handler, I would define the event handler on the activity itself.