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

十年热恋 提交于 2019-12-10 09:43:37

问题


Is this possible? I know it is for MS since they 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.


回答1:


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}" />



回答2:


The Pro WF : Windows Workflow in .Net 4 book by Bruce Bukovics also has lots of examples. You might want to check that out.




回答3:


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.



来源:https://stackoverflow.com/questions/3104929/how-can-i-write-a-custom-workflow-4-code-activity-that-includes-a-body-block

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