Search code examples
c#wpfwpf-controlsworkflow-foundationuipath

Passing variable through context.ScheduleActivity() in custom WPF component


I have created an activity to mimic the sequence activity in WPF using the below code with help from Windows Workflow Custom Sequence Activity

using System.Activities;
using System.Activities.Statements;
using System.Collections.ObjectModel;
using System.ComponentModel;

namespace Custom_Activities
{
    [Designer("System.Activities.Core.Presentation.SequenceDesigner, System.Activities.Core.Presentation")]
    public class Scoped_Activity_Scope : NativeActivity
    {
        private string TestVariable = "testing testing";
        private Sequence innerSequence = new Sequence();

        [Browsable(false)]
        public Collection<Activity> Activities
        {
            get 
            {
                return innerSequence.Activities;
            }
        }

        [Browsable(false)]
        public Collection<Variable> Variables
        {
            get
            {
                return innerSequence.Variables;
            }
        }

        protected override void CacheMetadata(NativeActivityMetadata metadata)
        {
            metadata.AddImplementationChild(innerSequence);
        }

        protected override void Execute(NativeActivityContext context)
        {
            System.Console.WriteLine("Scope Executing");
            context.ScheduleActivity(innerSequence);
        }

    }
}

I also have created a second custom activity below

using System.Activities;

namespace Custom_Activities
{
    public sealed class Scoped_Activity : Scoped_Activity_Template
    {
        protected override void Execute(CodeActivityContext context)
        {
            System.Console.WriteLine("Scope Activity Executing");
            //System.Console.WriteLine(testVariable);
        }
    }
}

When I run the code as below

enter image description here

I get the desired output

Scope Executing

Scope Activity Executing

How can I pass the variable testVariable from the class Scoped_Activity_Scope for use within Scoped_Activity as per the line of code commented out?


Solution

  • In the Scoped_Activity_Scope within the Execute method I added the following code to iterate through the activities within innerSequence

    foreach(Activity a in innerSequence.Activities)
    {
        if (a.GetType().IsSubclassOf(typeof(UiPath_Activities_Templates.Scoped_Activity_Template)))
        {
            Scoped_Activity_Template vet = null;
            vet = (Scoped_Activity_Template) a;
            vet.UpdateTestVariable("changed");
        }
    }
    

    And in the class 'Scoped_Activity_Template' which 'Scoped_Activity' inherits from (this class was previously empty) I added the following code

    public static string TestVariable = "testing";
    
    public void UpdateTestVariable(string newValue)
    {
        TestVariable = newValue;
    }
    

    That way the inherited class Scoped_Activity has access to the variable TestVariable.

    When the loop iterates over the activities in innerSequence it checks if it inherits from Scoped_Activity_Template it calls the method UpdateTestVariable to update the variable.

    When the class Scoped_Activity is then executed it will have the updated variable.