I'm looking at rehosting the workflow designer. I want to be able to run some code whenever the user adds an activity to the designer canvass. Is there an event that fires when the user adds an activity at de开发者_C百科sign time? Or is there an event on the activity that I can consume? Thanks!
For anyone who might stumble onto this, here's what I figured out...
First off, when creating the workflow designer, you need to subscribe to the ModelChanged event.
_workflowDesigner = new WorkflowDesigner();
_workflowDesigner.Load(new Sequence());
ModelService ms = _workflowDesigner.Context.Services.GetService<ModelService>();
if (ms != null)
ms.ModelChanged += new EventHandler<ModelChangedEventArgs>(ms_ModelChanged);
My event handler looks like this...
void ms_ModelChanged(object sender, ModelChangedEventArgs e)
{
if (e.ItemsAdded != null && e.ItemsAdded.Count<ModelItem>() == 1)
{
ModelItem item = e.ItemsAdded.FirstOrDefault<ModelItem>();
var test = item.GetCurrentValue() as MyActivityType;
if (test != null && test.Id == null)
{
//do whatever initialization logic is needed here
}
}
}
I need to give credit to this source for pointing me in the right direction.
One thing to be careful of - when you move an activity within the model, two events are raised, a remove and an add. At this point, I don't need to worry about whether I'm adding or moving an activity as I can tell whether it has been initialized, but if you need to know whether something has really been added to the model, you might need to track both events.
精彩评论