locked
Creating Custom IF Activity RRS feed

  • Question

  • Hello Guys,<p>Due to some requirements (moreover some technical constraint) I deceided to write my own IF activity. I Copied metadata of If Activity using object browser. Which looks like this</p>
    

    public class IfActivity : NativeActivity
        {
            // Summary:
            //     Creates a new instance of the System.Activities.Statements.If class.
            [TargetedPatchingOptOut("Performance critical to inline this type of method across NGen image boundaries")]
            public IfActivity();
            //
            // Summary:
            //     Creates a new instance of the System.Activities.Statements.If class using
            //     the specified condition.
            //
            // Parameters:
            //   condition:
            //     The execution condition.
            public IfActivity(Activity<bool> condition);
            //
            // Summary:
            //     Creates a new instance of the System.Activities.Statements.If class using
            //     the specified condition.
            //
            // Parameters:
            //   condition:
            //     The execution condition.
            public IfActivity(Expression<Func<ActivityContext, bool>> condition);
            //
            // Summary:
            //     Creates a new instance of the System.Activities.Statements.If class using
            //     the specified condition.
            //
            // Parameters:
            //   condition:
            //     The execution condition.
            public IfActivity(InArgument<bool> condition);
    
            // Summary:
            //     The condition that determines which child activity to execute. The System.Activities.Statements.If.Then
            //     activity executes if the condition resolves to true. The System.Activities.Statements.If.Else
            //     activity executes if the condition resolves to false.
            //
            // Returns:
            //     The execution condition.
            [RequiredArgument]
            [DefaultValue("")]
            public InArgument<bool> Condition { get; set; }
            //
            // Summary:
            //     The activity to be executed if the activity’s System.Activities.Statements.If.Condition
            //     resolves to false.
            //
            // Returns:
            //     The child activity.
            [DependsOn("Then")]
            [DefaultValue("")]
            public Activity Else { get; set; }
            //
            // Summary:
            //     The activity to be executed if the activity’s System.Activities.Statements.If.Condition
            //     resolves to true.
            //
            // Returns:
            //     The child activity.
            [DependsOn("Condition")]
            [DefaultValue("")]
            public Activity Then { get; set; }
    
            //protected override void CacheMetadata(NativeActivityMetadata metadata);
            protected override void Execute(NativeActivityContext context)
            {
    
            }
        }
    

    Now I am not sure if condition is true (or false) how to execute Then (or Else) activity?

    Or anyother way to acheive this?

    Note: I am not concerned about design view as We have our own design view application (web application).

    regards,

     

     


    Software Engineer
    Tuesday, January 24, 2012 10:33 AM

Answers

  • Hi,

    if I understand you right you want to know when to execute the 'Then' or the 'Else' activity, right? If so, just look at your comments. It's all there.
    If your 'Condition' is true you will execute the 'Then' activity in the 'Execute' method. Otherwise you will execute the 'Else' activity.

    Here's a sample:

    protected override void Execute(NativeActivityContext context)
    {
        if (this.Condition)
        {
            context.ScheduleActivity(this.Then);
        }
        else
        {
            context.ScheduleActivity(this.Else)
        }
    }
    


    Best Regards. Please Mark Answered, If my solution solves your problem.
    Tuesday, January 24, 2012 10:59 AM

All replies

  • Hi,

    if I understand you right you want to know when to execute the 'Then' or the 'Else' activity, right? If so, just look at your comments. It's all there.
    If your 'Condition' is true you will execute the 'Then' activity in the 'Execute' method. Otherwise you will execute the 'Else' activity.

    Here's a sample:

    protected override void Execute(NativeActivityContext context)
    {
        if (this.Condition)
        {
            context.ScheduleActivity(this.Then);
        }
        else
        {
            context.ScheduleActivity(this.Else)
        }
    }
    


    Best Regards. Please Mark Answered, If my solution solves your problem.
    Tuesday, January 24, 2012 10:59 AM
  • Thanks Horizon,

    Actually I was asking how to execute as I didn't find any method execute method in context. I was looking for something "ActivityExecutionContext" but didn't find this in any namespace.

    But Your sample code gave me what I was looking for.

    Thanks again.


    Software Engineer
    Tuesday, January 24, 2012 11:29 AM