How can I write a custom WorkFlow 4 Code Activity that includes a “Body Block”?

 ̄綄美尐妖づ 提交于 2019-12-05 19:05:56

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!