How can I write a custom WorkFlow 4 Code Activity that includes a "Body Block"?
Is this possible? I know it is for MS since th开发者_如何学Cey have WF activity packs but I'm not sure how it's done. It would be nice to be able to have Activities with Body blocks to insert other activities, buttons, etc. If not too much trouble and/or time consuming that is.
Its easy enough if you follow a few rules. Here's an example of a NativeActivity that has a child:
[Designer(typeof(MyActivityDesigner)), ContentProperty("Child")]
public sealed class MyActivity :
NativeActivity, IActivityTemplateFactory
{
// this "activity delegate" holds our child activity
public ActivityAction Child { get; set; }
// may be necessary to do this
protected override void
CacheMetadata(NativeActivityMetadata metadata)
{
metadata.AddDelegate(Child);
}
protected override void
Execute(NativeActivityContext context)
{
// do some work here, then
context.ScheduleAction(Child);
}
// better to use a template factory than a constructor to do this!
Activity IActivityTemplateFactory
.Create(System.Windows.DependencyObject target)
{
return new MyActivity
{
// HAVE to have this set, or it fails in the designer!
Child = new ActivityAction()
};
}
}
Note a few things: We use an Activity Delegate type to hold our child. Second, we implement IActivityTemplateFactory to configure our activity for the designer. Its always better/more stable to do this than set stuff up in the constructor. We will be binding to a property of the delegate, so we have to set an instance; otherwise the binding will fail.
When we execute, all you have to do is schedule your child when appropriate and return. You shouldn't block, of course.
Then, in the designer, you'd bind to Child like this:
<sap:WorkflowItemPresenter
HintText="Add children here!"
Item="{Binding Path=ModelItem.Child.Handler}" />
The Pro WF : Windows Workflow in .Net 4 book by Bruce Bukovics also has lots of examples. You might want to check that out.
You need to start with a NativeActivity instead of a CodeActivity. The NativeActivity lets you schedule child activities through its execution context. There is no template for the NativeActivity, instead you just create a class and derive from NativeActivity.
精彩评论