WF4 While activity condition in code

做~自己de王妃 提交于 2020-01-04 06:55:36

问题


I am building a workflow in my code, and I don't know how I can add a simple (While) condition. Tried to figure out how, but no luck with it, search on the internet but no luck either.

This is a simplified version of what I am trying to do:

ActivityBuilder ab = new ActivityBuilder();
ab.Implementation = new Sequence()
{
  Variables = 
  {
     new Variable<int>("StepNo", 0)
  },

  Activities =
  {
    new While()
    {
      Condition = <the_condition>

      Body = 
      {
        //Some logic here and the StepNo is increased
      }
    }
  }   
}

The While condition should be something like "StepNo < 10". Any idea how can this be made?


回答1:


var stepNo = new Variable<int>("stepNo", 0);

var activity = new Sequence
{
    Variables = 
    {
        stepNo
    },

    Activities = 
    {
        new While
        {
            Condition = new LessThan<int,int,bool>
            {
                Left = stepNo,
                Right = 10
            },

            Body = new Sequence
            {
                Activities = 
                {
                    new Assign<int>
                    {
                        To = stepNo,
                        Value = new Add<int, int, int>
                        {
                            Left = stepNo,
                            Right = 1
                        }
                    },

                    new WriteLine
                    { 
                        Text = new VisualBasicValue<string>("\"Step: \" & stepNo") 
                    }
                }
            }
        }
    }
};

Or a version without expression activities but only with VisualBasicValue, which is also an activity:

var stepNo = new Variable<int>("stepNo", 0);

var activity = new Sequence
{
    Variables = 
    {
        stepNo
    },

    Activities = 
    {
        new While
        {
            Condition = new VisualBasicValue<bool>("stepNo < 10"),

            Body = new Sequence
            {
                Activities = 
                {
                    new Assign<int>
                    {
                        To = stepNo,
                        Value = new VisualBasicValue<int>("stepNo + 1")
                    },

                    new WriteLine
                    { 
                        Text = new VisualBasicValue<string>("\"Step: \" & stepNo") 
                    }
                }
            }
        }
    }
};


来源:https://stackoverflow.com/questions/8066545/wf4-while-activity-condition-in-code

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