none
Custom Sequence Activity

    Question

  • Is it possible to create a custom Sequence Activity? I am looking to create an Activity which supports multiple children without having to add a Sequence Activity as the first child.  The primary reason we want this is because we want to apply a custom designer which would tightly pack the children together.  This would help considerably with the readability of sequences with many simple design children.
    Sunday, November 22, 2009 4:34 AM

Answers

All replies

  • Yes, Sequence is just a composite. There is nothing special about WF 4 Sequence. Is just a composite activity that has a collection of child activities and schedules them when its executed. The activity that we have in the box does not use any "internal" tricks and could be written by any WF user (our simplified activity authoring story is one of the beauties of WF4)

    All you need to create a similar activity is to create an activity that derives from NativeActivity, add a property to contain the collection of children and then schedule the children at execution.

    The code below shows how to implement a simple Sequence activity in WF 4:

    public class MySequence : NativeActivity
    {
        Collection<Activity> activities;
        Collection<Variable> variables;
        Variable<int> lastIndex;
        CompletionCallback onChildComplete;
    
        public Collection<Variable> Variables
        {
            get
            {
                if (this.variables == null)
                    this.variables = new Collection<Variable>();
                return this.variables;
            }
        }
    
        public Collection<Activity> Activities
        {
            get
            {
                if (this.activities == null)
                    this.activities = new Collection<Activity>();
                return this.activities;
            }
        }
    
        public MySequence(): base()
        {
            this.lastIndex = new Variable<int>();
            this.onChildComplete = new CompletionCallback(InternalExecute);
        }
    
        protected override void CacheMetadata(NativeActivityMetadata metadata)
        {
            metadata.AddImplementationVariable(this.lastIndex);
            base.CacheMetadata(metadata);
        }
    
        protected override void Execute(NativeActivityContext context)
        {
            if (this.Activities.Count > 0)
            {
                this.lastIndex.Set(context, 0);
    
                context.ScheduleActivity(this.activities[0], this.onChildComplete);
            }
        }
    
        void InternalExecute(NativeActivityContext executionContext, ActivityInstance completedInstance)
        {            
            int nextChildIndex = this.lastIndex.Get(executionContext) + 1;            
    
            if (nextChildIndex < this.Activities.Count)
            {                
                executionContext.ScheduleActivity(this.Activities[nextChildIndex], this.onChildComplete);
    
                this.lastIndex.Set(executionContext, nextChildIndex);
            }
        }      
    }
    

    We have some other composites samples that you can look at in the activity library in the SDK (under %SamplesRoot%\WF\Scenario\ActivityLibrary)

    Leon Welicki
    Program Manager, WF

    Monday, November 23, 2009 5:37 AM
  • Cool, are there any template/example sequence designers?

    Monday, November 23, 2009 5:41 AM
  • I did some investigation and I sure have no clue where to start with creating a designer.  What's interesting is that I figured I would be able to at least apply the normal Sequence Designer to this but I can't find where it is or what its call either.  In fact I can't figure out how the Core Activities are assigned their associated designers at all.
    Monday, November 23, 2009 6:40 AM
  • There are some great samples and blog posts about Custom Designers and the Designer Programming Model.

    The sample of value for you here seems to be the CustomActivityDesigners one under the Beta2 SDK samples that can be downloaded from here: http://social.msdn.microsoft.com/Forums/en-US/wfprerelease/thread/f54e8eb2-29d9-46f6-ad96-766fe6fad333

    The Out of Box Sequence Designer is internal and so are the other out of Box activity designers. So you wont be able to use them directly. You would have to write your own custom designer. If you want to ude the Sequence styling though, you can take a look at this post:
    http://blogs.msdn.com/kushals/archive/2009/11/22/using-the-sequence-styling.aspx


    THe other blogs that can always provide you a lot more information about Designers are:

    http://blogs.msdn.com/kushals
    http://blogs.msdn.com/mwinkle/
    http://blogs.msdn.com/cathyk/

    Thanks,
    Kushal.




    Kushal Shah - This posting is provided "AS IS" with no warranties, and confers no rights
    • Proposed as answer by kushals Monday, November 23, 2009 6:55 AM
    • Marked as answer by Mike Oliszewski Monday, November 23, 2009 7:37 AM
    Monday, November 23, 2009 6:55 AM