Windows Workflow Custom Sequence Activity
I'm working with Windows Workflow 4, and I need to create a C# activity that, basically, inherits from the Sequence activity. I want it to look just like the Sequence activity, so a user can drag and drop other activities onto it from the designer. But, it acts differently in the code (maybe I want to run them in a different order, or do special actions betwe开发者_StackOverflowen each one, it shouldn't matter).
How can I do this? I see a similar question was asked about this, and only one person responded with a suggestion that only applies to Windows Workflow 3. In version 4, a sequence activity can't be inherited from, to say the least.
This doesn't seem like a very far fetched concept. A Sequence activity is provided as a built in activity. So, it seems logical that it should be reproducible, or at least inheritable, so I can have a customized version of a Sequence activity.
Anyone have any ideas?
The "System.Activities.Core.Presentation.SequenceDesigner" designer is already available in WF 4. One can then compose a Sequence activity and use this designer for the outer class.
Here's a working example:
using System.Activities;
using System.Activities.Statements;
using System.Collections.ObjectModel;
using System.ComponentModel;
[Designer("System.Activities.Core.Presentation.SequenceDesigner, System.Activities.Core.Presentation")]
public class MySeq : NativeActivity
{
private Sequence innerSequence = new Sequence();
[Browsable(false)]
public Collection<Activity> Activities
{
get
{
return innerSequence.Activities;
}
}
[Browsable(false)]
public Collection<Variable> Variables
{
get
{
return innerSequence.Variables;
}
}
protected override void CacheMetadata(NativeActivityMetadata metadata)
{
metadata.AddImplementationChild(innerSequence);
}
protected override void Execute(NativeActivityContext context)
{
context.ScheduleActivity(innerSequence);
}
}
This is forwarding the real behavior on to a private innerSequence, which might make this code seem useless, but note that in the Execute method it gives us a chance to do things before and after execution. If we want to provide customized behavior, we'd have to implement it instead of forwarding to an inner private activity.
精彩评论